Convert.ToBase64CharArray() Method in C#

The Convert.ToBase64CharArray() method in C# converts a subset of an 8-bit unsigned integer array to an equivalent subset of a Unicode character array encoded with base-64 digits. This method provides more control than the standard Convert.ToBase64String() by allowing you to specify input and output positions and work directly with character arrays.

Syntax

Following is the syntax −

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

Parameters

  • inArray − An input array of 8-bit unsigned integers (bytes).
  • offsetIn − A position within the input array to start conversion.
  • length − The number of elements from the input array to convert.
  • outArray − An output array of Unicode characters to store the base-64 encoded result.
  • offsetOut − A position within the output array to start writing the encoded characters.

Return Value

The method returns an int representing the number of characters written to the output array.

Base64 Encoding Process Byte Array [5, 10, 15, 20] Char Array ['B','Q','o','P'] Convert Binary data encoded to Base64 characters Returns count of characters written

Example

Converting Byte Array to Base64 Character Array

using System;

public class Demo {
   public static void Main() {
      byte[] val1 = {5, 10, 15, 20, 25, 30};
      
      // Calculate required output array size
      long arrLen = (long)((4.0d / 3.0d) * val1.Length);
      if (arrLen % 4 != 0)
         arrLen += 4 - arrLen % 4;
      
      char[] base64CharArray = new char[arrLen];
      
      // Convert byte array to base64 character array
      int res = Convert.ToBase64CharArray(val1, 0, val1.Length, base64CharArray, 0);
      
      Console.WriteLine("Number of characters written: " + res);
      Console.WriteLine("Original byte array: [" + string.Join(", ", val1) + "]");
      Console.WriteLine("Base64 character array: " + new string(base64CharArray));
   }
}

The output of the above code is −

Number of characters written: 8
Original byte array: [5, 10, 15, 20, 25, 30]
Base64 character array: BQoPFBke

Using Offset Parameters

Converting Partial Array with Custom Offsets

using System;

public class Demo {
   public static void Main() {
      byte[] inputBytes = {1, 2, 3, 4, 5, 6, 7, 8, 9};
      char[] outputChars = new char[20];
      
      // Fill output array with asterisks initially
      for (int i = 0; i < outputChars.Length; i++) {
         outputChars[i] = '*';
      }
      
      // Convert only 3 bytes starting from index 2, place result at index 5
      int charsWritten = Convert.ToBase64CharArray(inputBytes, 2, 3, outputChars, 5);
      
      Console.WriteLine("Input bytes: [" + string.Join(", ", inputBytes) + "]");
      Console.WriteLine("Converting 3 bytes from index 2: [3, 4, 5]");
      Console.WriteLine("Characters written: " + charsWritten);
      Console.WriteLine("Output array: " + new string(outputChars));
      Console.WriteLine("Base64 part only: " + new string(outputChars, 5, charsWritten));
   }
}

The output of the above code is −

Input bytes: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Converting 3 bytes from index 2: [3, 4, 5]
Characters written: 4
Output array: *****AwQF***************
Base64 part only: AwQF

How It Works

Base64 encoding converts binary data into ASCII characters using a 64-character alphabet. Every 3 bytes (24 bits) are encoded into 4 base64 characters (24 bits). The method handles padding automatically when the input length is not divisible by 3.

Input Bytes Binary (24 bits) Base64 Groups (6 bits each) Base64 Characters
[3, 4, 5] 000000110000010000000101 000000 110000 010000 000101 A w Q F

Conclusion

The Convert.ToBase64CharArray() method provides fine-grained control over base64 encoding by allowing you to specify exact positions in both input and output arrays. This method is useful when working with character arrays directly or when you need to encode only portions of byte arrays into specific positions of pre-allocated character arrays.

Updated on: 2026-03-17T07:04:35+05:30

331 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements