C# Convert FromBase64String() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

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="));
CallWhat 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

ParameterDescription
sRequired. Base64 text to decode.

Return Value

Return ValueDescription
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));
    }
}

How It Works

  1. AQID in. Short Base64 sample.
  2. Bytes out. Decodes to 1, 2, 3.
  3. Join for print. Easy to read in the console.
  4. 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));
    }
}

How It Works

  1. SGk=. Base64 for UTF-8 "Hi".
  2. FromBase64String. Restores the two bytes.
  3. GetString. Turns bytes back into text.
  4. 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));
    }
}

How It Works

  1. Encode first. Original bytes become Base64.
  2. Decode second. FromBase64String restores them.
  3. Print both. Text and byte list for clarity.
  4. 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.

Next: FromHexString()

Decode a hex string into a byte array (.NET 5+) with System.Convert.

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

9 people found this page helpful