C# Convert TryToBase64Chars() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

In C#, Convert.TryToBase64Chars encodes bytes into a destination Span<char> / char buffer and returns whether it succeeded.

If the destination is too small, it returns false instead of throwing. Available on modern .NET builds.

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

Syntax

Pass source bytes and a destination char span.

C#
byte[] data = { 1, 2, 3 };
Span<char> dest = stackalloc char[8];
bool ok = Convert.TryToBase64Chars(data, dest, out int written);
CallWhat it does
Convert.TryToBase64Chars(data, dest, out written)true if dest was large enough.

On success, written is the Base64 length. On failure, written is 0 and no throw.

Library declarations
C#
public static bool TryToBase64Chars(ReadOnlySpan<byte> bytes,
    Span<char> chars, out int charsWritten,
    Base64FormattingOptions options = Base64FormattingOptions.None);

Parameters

ParameterDescription
bytesRequired. Source bytes.
charsRequired. Destination char span.
charsWrittenOut. Characters written on success.

Return Value

Return ValueDescription
trueEncoding succeeded.
falseDestination too small (or cannot complete).

C# Examples

Example 1 — Encode with Room to Spare

Use a large enough stack buffer.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 1, 2, 3 };
        Span<char> dest = stackalloc char[8];
        bool ok = Convert.TryToBase64Chars(data, dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(new string(dest.Slice(0, written)));
    }
}

How It Works

  1. stackalloc. Temporary char buffer on the stack.
  2. ok true. Eight chars fit AQID.
  3. Slice written. Build a string from used chars only.
  4. Same text. Matches ToBase64String output.

Example 2 — False When Buffer Is Tiny

Show false result for a 1-char destination.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 1, 2, 3 };
        Span<char> dest = stackalloc char[1];
        bool ok = Convert.TryToBase64Chars(data, dest, out int written);
        Console.WriteLine(ok);
        Console.WriteLine(written);
    }
}

How It Works

  1. Tiny dest. One char cannot hold Base64 output.
  2. false. Try API refuses without throwing.
  3. written 0. Nothing reliable was stored.
  4. Retry larger. Allocate a bigger span and try again.

Example 3 — Print Written Count

Focus on charsWritten after success.

C#
using System;

class Program {
    static void Main() {
        byte[] data = { 9, 9 };
        char[] buf = new char[16];
        bool ok = Convert.TryToBase64Chars(data, buf, out int written);
        Console.WriteLine(ok + " " + written);
        Console.WriteLine(new string(buf, 0, written));
    }
}

How It Works

  1. Two bytes. Small input keeps output short.
  2. True 4. Success and character count together.
  3. CQk=. Encoded form with padding.
  4. char[] span. Arrays cast to spans automatically.

Frequently Asked Questions

On modern .NET (span-based Convert helpers). Use ToBase64String on older targets if needed.
Here it usually means the destination span was too small.
No—Base64 encoding only.
ToBase64CharArray / ToBase64String.
No—stackalloc Span<char> is safe in modern C#.

Next: TryToHexString()

Try to write hex characters into a destination span with System.Convert.

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

7 people found this page helpful