In C#, Convert.FromBase64String reads Base64 text and returns the original byte[].
Invalid characters or bad padding throw FormatException. Prefer TryFromBase64String when input may be wrong.
This is a static member of System.Convert. You call it with the type name, such as Convert.FromBase64String(...). Convert is the everyday API for converting between common CLR types in C#.
Syntax
Pass a Base64 string.
C#
byte[] data = Convert.FromBase64String("AQID");
string text = System.Text.Encoding.UTF8.GetString(
Convert.FromBase64String("SGk="));
Call
What it does
Convert.FromBase64String("AQID")
Decode to bytes {1,2,3}.
Whitespace is ignored in typical Base64 strings. Completely invalid text throws.
Library declarations
C#
public static byte[] FromBase64String(string s);
Parameters
Parameter
Description
s
Required. Base64 text to decode.
Return Value
Return Value
Description
byte[]
Decoded bytes.
C# Examples
Example 1 — Base64 Back to Bytes
Decode AQID and print each byte.
C#
using System;
class Program {
static void Main() {
byte[] data = Convert.FromBase64String("AQID");
Console.WriteLine(string.Join(",", data));
}
}
Output
1,2,3
How It Works
AQID in. Short Base64 sample.
Bytes out. Decodes to 1, 2, 3.
Join for print. Easy to read in the console.
Matches encode. Inverse of ToBase64String on those bytes.
Example 2 — Decode Then GetString
Restore a short UTF-8 message.
C#
using System;
using System.Text;
class Program {
static void Main() {
byte[] data = Convert.FromBase64String("SGk=");
Console.WriteLine(Encoding.UTF8.GetString(data));
}
}
Output
Hi
How It Works
SGk=. Base64 for UTF-8 "Hi".
FromBase64String. Restores the two bytes.
GetString. Turns bytes back into text.
Padding kept. The = is part of valid Base64.
Example 3 — Encode Then Decode
Prove a round-trip with ToBase64String.
C#
using System;
class Program {
static void Main() {
byte[] original = { 9, 8, 7 };
string b64 = Convert.ToBase64String(original);
byte[] back = Convert.FromBase64String(b64);
Console.WriteLine(b64);
Console.WriteLine(string.Join(",", back));
}
}
Output
CQgH
9,8,7
How It Works
Encode first. Original bytes become Base64.
Decode second. FromBase64String restores them.
Print both. Text and byte list for clarity.
Lossless. Values 9,8,7 return unchanged.
Frequently Asked Questions
FormatException is thrown.
Yes—TryFromBase64String.
Most whitespace is ignored in FromBase64String.
Returns an empty byte array.
FromBase64CharArray decodes from a character buffer.