Home C# Convert Methods FromBase64CharArray() C# Convert FromBase64CharArray() Method 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.
char[] chars = "AQID".ToCharArray();
byte[] data = Convert.FromBase64CharArray(chars, 0, chars.Length); Call What 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 public static byte[] FromBase64CharArray(char[] inArray, int offset, int length); Parameters Parameter Description inArray Required. Characters holding Base64 text. offset / length Slice of the array to decode.
Return Value Return Value Description byte[] Decoded bytes.
C# Examples 1. Decode chars 2. Slice 3. Round-trip Example 1 — Char Array to Bytes Decode AQID stored in a char[].
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 ToCharArray. Materialize Base64 as chars.Full length. Offset 0 covers every character.Bytes 1,2,3. Same result as FromBase64String.Join output. Easy console verification.Example 2 — Decode a Middle Slice Skip a prefix character before decoding.
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 Prefix #. Not part of the Base64 payload.offset 1. Start at A.length 4. Read AQID only.Same bytes. Still decodes to 1,2,3.Example 3 — CharArray Encode Path Encode with ToBase64CharArray, then decode.
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 Encode to chars. ToBase64CharArray fills buf.Decode with n. Use only the written length.Print Base64. Shows BQY=.Bytes restored. 5 and 6 return.Frequently Asked Questions When use CharArray vs String? CharArray when data already lives in a char buffer or you need a slice.
What if Base64 is invalid? FormatException is thrown.
Try alternative? TryFromBase64Chars returns false instead of throwing.
Does length include padding? Yes—pass the full Base64 character count including =.
Empty length? Returns an empty byte array.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion