This documentation is archived and is not being maintained.

Convert.ToBase64CharArray Method (Byte[], Int32, Int32, Char[], Int32)

Converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. Parameters specify the subsets as offsets in the input and output arrays, and the number of elements in the input array to convert.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

public static int ToBase64CharArray(
	byte[] inArray,
	int offsetIn,
	int length,
	char[] outArray,
	int offsetOut


Type: System.Byte[]
An input array of 8-bit unsigned integers.
Type: System.Int32
A position within inArray.
Type: System.Int32
The number of elements of inArray to convert.
Type: System.Char[]
An output array of Unicode characters.
Type: System.Int32
A position within outArray.

Return Value

Type: System.Int32
A 32-bit signed integer containing the number of bytes in outArray.


inArray or outArray is null.


offsetIn, offsetOut, or length is negative.


offsetIn plus length is greater than the length of inArray.


offsetOut plus the number of elements to return is greater than the length of outArray.

The subset of length elements of inArray starting at position offsetIn, are taken as a numeric value and converted to a subset of elements in outArray starting at position offsetOut. The return value indicates the number of converted elements in outArray. The subset of outArray consists of base-64 digits.

The base-64 digits in ascending order from zero are the uppercase characters "A" to "Z", the lowercase characters "a" to "z", the numerals "0" to "9", and the symbols "+" and "/". The valueless character, "=", is used for trailing padding.

The offset and length parameters are 32-bit signed numbers. The offsetIn and offsetOut parameters are zero-based array positions.

Important noteImportant

The ToBase64CharArray method is designed to process a single byte array that contains all the data to be encoded. To create a base-64 character array from a byte stream, use the System.Security.Cryptography.ToBase64Transform class.

The following example demonstrates using the ToBase64CharArray method to UUencode (encode in base 64) a binary stream, then save the encoding to a file.

public void EncodeWithCharArray() {
   System.IO.FileStream inFile;    
   byte[]             binaryData;

   try {
      inFile = new System.IO.FileStream(inputFileName,
      binaryData = new Byte[inFile.Length];
      long bytesRead = inFile.Read(binaryData, 0,
                           (int) inFile.Length);
   catch (System.Exception exp) {
      // Error creating stream or reading from it.
      System.Console.WriteLine("{0}", exp.Message);

   // Convert the binary input into Base64 UUEncoded output.
   // Each 3 byte sequence in the source data becomes a 4 byte
   // sequence in the character array. 
   long arrayLength = (long) ((4.0d/3.0d) * binaryData.Length);

   // If array length is not divisible by 4, go up to the next
   // multiple of 4.
   if (arrayLength % 4 != 0) {
      arrayLength += 4 - arrayLength % 4;

   char[] base64CharArray = new char[arrayLength];
   try {
   catch (System.ArgumentNullException) {
      System.Console.WriteLine("Binary data array is null.");
   catch (System.ArgumentOutOfRangeException) {
      System.Console.WriteLine("Char Array is not large enough.");

   // Write the UUEncoded version to the output file.
   System.IO.StreamWriter outFile; 
   try {
      outFile = new System.IO.StreamWriter(outputFileName,
   catch (System.Exception exp) {
      // Error creating stream or writing to it.
      System.Console.WriteLine("{0}", exp.Message);

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.