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);
Call
What 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
Parameter
Description
bytes
Required. Source bytes.
chars
Required. Destination char span.
charsWritten
Out. Characters written on success.
Return Value
Return Value
Description
true
Encoding succeeded.
false
Destination 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)));
}
}
Output
True
AQID
How It Works
stackalloc. Temporary char buffer on the stack.
ok true. Eight chars fit AQID.
Slice written. Build a string from used chars only.
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);
}
}
Output
False
0
How It Works
Tiny dest. One char cannot hold Base64 output.
false. Try API refuses without throwing.
written 0. Nothing reliable was stored.
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));
}
}
Output
True 4
CQk=
How It Works
Two bytes. Small input keeps output short.
True 4. Success and character count together.
CQk=. Encoded form with padding.
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.