C# Convert FromBase64CharArray() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.FromBase64CharArray decodes Base64 characters stored in a char[].

You pass offset and length so you can decode a slice without building a new string first.

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

Syntax

Pass the char buffer with offset and length.

C#
char[] chars = "AQID".ToCharArray();
byte[] data = Convert.FromBase64CharArray(chars, 0, chars.Length);
CallWhat it does
Convert.FromBase64CharArray(chars, 0, chars.Length)Decode the whole buffer.

Invalid Base64 throws FormatException. TryFromBase64Chars is the soft-fail span API.

Library declarations
C#
public static byte[] FromBase64CharArray(char[] inArray, int offset, int length);

Parameters

ParameterDescription
inArrayRequired. Characters holding Base64 text.
offset / lengthSlice of the array to decode.

Return Value

Return ValueDescription
byte[]Decoded bytes.

C# Examples

Example 1 — Char Array to Bytes

Decode AQID stored in a char[].

C#
using System;

class Program {
    static void Main() {
        char[] chars = "AQID".ToCharArray();
        byte[] data = Convert.FromBase64CharArray(chars, 0, chars.Length);
        Console.WriteLine(string.Join(",", data));
    }
}

How It Works

  1. ToCharArray. Materialize Base64 as chars.
  2. Full length. Offset 0 covers every character.
  3. Bytes 1,2,3. Same result as FromBase64String.
  4. Join output. Easy console verification.

Example 2 — Decode a Middle Slice

Skip a prefix character before decoding.

C#
using System;

class Program {
    static void Main() {
        char[] chars = "#AQID".ToCharArray();
        byte[] data = Convert.FromBase64CharArray(chars, 1, 4);
        Console.WriteLine(string.Join(",", data));
    }
}

How It Works

  1. Prefix #. Not part of the Base64 payload.
  2. offset 1. Start at A.
  3. length 4. Read AQID only.
  4. Same bytes. Still decodes to 1,2,3.

Example 3 — CharArray Encode Path

Encode with ToBase64CharArray, then decode.

C#
using System;

class Program {
    static void Main() {
        byte[] original = { 5, 6 };
        char[] buf = new char[8];
        int n = Convert.ToBase64CharArray(original, 0, 2, buf, 0);
        byte[] back = Convert.FromBase64CharArray(buf, 0, n);
        Console.WriteLine(new string(buf, 0, n));
        Console.WriteLine(string.Join(",", back));
    }
}

How It Works

  1. Encode to chars. ToBase64CharArray fills buf.
  2. Decode with n. Use only the written length.
  3. Print Base64. Shows BQY=.
  4. Bytes restored. 5 and 6 return.

Frequently Asked Questions

CharArray when data already lives in a char buffer or you need a slice.
FormatException is thrown.
TryFromBase64Chars returns false instead of throwing.
Yes—pass the full Base64 character count including =.
Returns an empty byte array.

Next: FromBase64String()

Decode a Base64 string back into a byte array with System.Convert.

FromBase64String() 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.

8 people found this page helpful