C# Convert TryFromBase64String() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.TryFromBase64String attempts to decode Base64 text into a destination byte buffer.

It returns false for invalid text or a too-small buffer instead of throwing FormatException.

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

Syntax

Pass the Base64 string and a destination byte span.

C#
Span<byte> dest = stackalloc byte[8];
bool ok = Convert.TryFromBase64String("AQID", dest, out int written);
CallWhat it does
Convert.TryFromBase64String(text, dest, out written)true when decode succeeds.

Newer .NET API. On older runtimes use FromBase64String inside try/catch.

Library declarations
C#
public static bool TryFromBase64String(string s, Span<byte> bytes, out int bytesWritten);

Parameters

ParameterDescription
sRequired. Base64 text.
bytesRequired. Destination buffer.
bytesWrittenOut. Bytes written on success.

Return Value

Return ValueDescription
trueDecode succeeded.
falseInvalid Base64 or buffer too small.

C# Examples

Example 1 — Decode Valid Base64

Try-decode AQID into a stack buffer.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        bool ok = Convert.TryFromBase64String("AQID", dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(string.Join(",", dest.Slice(0, written).ToArray()));
    }
}

How It Works

  1. Valid text. AQID is well-formed Base64.
  2. ok true. Decode filled the span.
  3. written slice. Print only produced bytes.
  4. 1,2,3. Expected payload.

Example 2 — False on Garbage

Show false for illegal Base64 characters.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        bool ok = Convert.TryFromBase64String("****", dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(written);
    }
}

How It Works

  1. Illegal stars. Not Base64 alphabet.
  2. false. No exception thrown.
  3. written 0. Treat output as unused.
  4. Safer UX. Good for validating user paste.

Example 3 — Contrast with FromBase64String

Catch FormatException from the throwing API.

C#
using System;

class Program {
    static void Main() {
        Span<byte> dest = stackalloc byte[8];
        Console.WriteLine(Convert.TryFromBase64String("AQID", dest, out _));
        try {
            Convert.FromBase64String("****");
        } catch (FormatException) {
            Console.WriteLine("threw");
        }
    }
}

How It Works

  1. Try succeeds. AQID decodes with true.
  2. Throwing API. FromBase64String rejects ****.
  3. FormatException. Catch prints threw.
  4. Pick Try. When bad input is expected.

Frequently Asked Questions

Use Try for untrusted input; From when failure should be exceptional.
Invalid Base64 or destination too small.
Yes—span-based Try helpers are on modern .NET.
Succeeds with zero bytes written.
TryFromBase64Chars for char spans.

Next: TryFromHexString()

Try to decode hex text into a byte span without throwing with System.Convert.

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

14 people found this page helpful