C# Convert TryFromBase64Chars() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.TryFromBase64Chars decodes a ReadOnlySpan<char> of Base64 text into a byte destination.

Returns false instead of throwing when the text is invalid or the buffer is too small.

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

Syntax

Pass source chars and a destination byte span.

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

Sibling of TryFromBase64String for char spans. Modern .NET required.

Library declarations
C#
public static bool TryFromBase64Chars(ReadOnlySpan<char> chars,
    Span<byte> bytes, out int bytesWritten);

Parameters

ParameterDescription
charsRequired. Base64 characters.
bytesRequired. Destination buffer.
bytesWrittenOut. Bytes written on success.

Return Value

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

C# Examples

Example 1 — Decode a Char Span

Try-decode AQID from a string as span.

C#
using System;

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

How It Works

  1. Span source. String implicitly becomes ReadOnlySpan<char>.
  2. ok true. Characters were valid Base64.
  3. Bytes listed. 1,2,3 printed.
  4. No string decode API required. Chars go straight in.

Example 2 — False for Bad Alphabet

Reject non-Base64 characters softly.

C#
using System;

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

How It Works

  1. @@@@. Outside the Base64 alphabet.
  2. false result. Soft failure path.
  3. written 0. Ignore destination contents.
  4. No throw. Unlike FromBase64CharArray.

Example 3 — Decode From String Slice

Use AsSpan to decode a substring region.

C#
using System;

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

How It Works

  1. Embedded text. AQID sits inside a larger string.
  2. AsSpan(2,4). Selects only the Base64 part.
  3. Decode ok. Same bytes as a plain AQID.
  4. No substring alloc. Span avoids new string().

Frequently Asked Questions

Chars takes a char span; String takes a string. Behavior is the same idea.
No—false means failure without an exception.
Also returns false.
May need FromBase64CharArray + try/catch instead.
Succeeds with zero bytes written.

Next: TryFromBase64String()

Try to decode a Base64 string without throwing on bad input with System.Convert.

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

13 people found this page helpful