C# Convert ToBase64CharArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.ToBase64CharArray encodes bytes into a char[] you allocate ahead of time.

It returns the number of characters written. Prefer ToBase64String when a simple string is enough.

This is a static member of System.Convert. You call it with the type name, such as Convert.ToBase64CharArray(...). Convert is the everyday API for converting between common CLR types in C#.

Syntax

Pass input bytes and an output char buffer with offsets.

C#
byte[] data = { 1, 2, 3 };
char[] buf = new char[8];
int written = Convert.ToBase64CharArray(data, 0, data.Length, buf, 0);
CallWhat it does
Convert.ToBase64CharArray(data, 0, len, buf, 0)Encode into buf starting at index 0.

Buffer must be large enough or ArgumentException is thrown. written tells you the used length.

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

Parameters

ParameterDescription
inArrayRequired. Source bytes.
offsetIn / lengthByte slice to encode.
outArray / offsetOutDestination char buffer and start index.

Return Value

Return ValueDescription
intNumber of Base64 characters written.

C# Examples

Example 1 — Encode into char[]

Fill a char buffer and print the written slice.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 1, 2, 3 };
        char[] buf = new char[8];
        int n = Convert.ToBase64CharArray(data, 0, data.Length, buf, 0);
        Console.WriteLine(n);
        Console.WriteLine(new string(buf, 0, n));
    }
}

How It Works

  1. Allocate buf. Eight chars is plenty for three bytes.
  2. written = 4. AQID needs four characters.
  3. new string. Build text from only the used slice.
  4. Same payload. Matches ToBase64String for {1,2,3}.

Example 2 — Write After a Prefix

Leave room at the start of the buffer.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 1, 2, 3 };
        char[] buf = new char[10];
        buf[0] = '#';
        int n = Convert.ToBase64CharArray(data, 0, data.Length, buf, 1);
        Console.WriteLine(n);
        Console.WriteLine(new string(buf, 0, n + 1));
    }
}

How It Works

  1. Prefix char. Index 0 holds '#'.
  2. offsetOut 1. Base64 starts at index 1.
  3. written 4. Same encoded length.
  4. Combined print. Shows #AQID together.

Example 3 — Encode Two Bytes Only

Limit length so only a prefix is encoded.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 1, 2, 3, 4 };
        char[] buf = new char[8];
        int n = Convert.ToBase64CharArray(data, 0, 2, buf, 0);
        Console.WriteLine(new string(buf, 0, n));
        Console.WriteLine(n);
    }
}

How It Works

  1. length 2. Only bytes 1 and 2 are encoded.
  2. AQI=. Short Base64 with padding.
  3. written count. Second line shows how many chars.
  4. Ignore tail. Bytes 3 and 4 stay out of the encoding.

Frequently Asked Questions

To reuse buffers or write into an existing character workspace.
ArgumentException is thrown.
How many Base64 characters were written.
Yes—TryToBase64Chars (span-based, returns bool).
ToBase64String when you just need a string.

Next: ToBase64String()

Encode a byte array as a Base64 string with System.Convert.

ToBase64String() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful