Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Strings and Text

Convert.ToBase64String() and FromBase64String()

Reference for Base64 in C#: Convert.ToBase64String, FromBase64String, span and TryFrom overloads, URL-safe Base64Url, padding rules and common mistakes.

Base64 writes arbitrary bytes as plain ASCII text using 64 safe characters (A-Z, a-z, 0-9, +, /) and = for padding. It is how binary data travels through places that only accept text: JSON fields, HTTP headers, data URLs, e-mail. Convert.ToBase64String encodes a byte array and Convert.FromBase64String decodes it back.

Base64 is an encoding, not encryption. Anyone can decode it, and it makes the data about one third larger (every 3 bytes become 4 characters). For URLs and file names, where +, / and = cause trouble, .NET 9 added System.Buffers.Text.Base64Url.

C#
string text = Convert.ToBase64String(new byte[] { 72, 105 });   // "SGk="
byte[] bytes = Convert.FromBase64String(text);

Try it

C# Encode and decode text

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Invalid input and URL-safe Base64

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public static string Convert.ToBase64String(byte[] inArray);
public static string Convert.ToBase64String(byte[] inArray, Base64FormattingOptions options);
public static string Convert.ToBase64String(ReadOnlySpan<byte> bytes, Base64FormattingOptions options = None);
public static byte[] Convert.FromBase64String(string s);
public static bool   Convert.TryFromBase64String(string s, Span<byte> bytes, out int bytesWritten);
public static bool   Convert.TryToBase64Chars(ReadOnlySpan<byte> bytes, Span<char> chars, out int charsWritten,
                                              Base64FormattingOptions options = None);

// System.Buffers.Text (.NET 9)
public static class Base64Url
{
    public static string EncodeToString(ReadOnlySpan<byte> source);
    public static byte[] DecodeFromChars(ReadOnlySpan<char> source);
}
Name Description
inArray / bytes The data to encode.
options InsertLineBreaks adds a line break every 76 characters; None produces one line.
s The Base64 text to decode. Whitespace (spaces, tabs, line breaks) is ignored.
bytes (out buffer) / bytesWritten Destination span and the number of bytes decoded.

Returns: the encoded string, the decoded byte[], or bool for the Try methods.

Errors / undefined behavior: FromBase64String throws FormatException if the length (ignoring whitespace) is not a multiple of 4, if it contains characters outside the alphabet, or if padding is misplaced. ArgumentNullException for null. TryFromBase64String returns false for the same problems or when the destination is too small, instead of throwing.

Usage notes

  • Text must become bytes first. Base64 encodes bytes, not strings. Choose the encoding explicitly (Encoding.UTF8) so both sides agree.
  • Padding is mandatory in classic Base64. "SGVsbG8" (without =) is rejected, as the second example shows. Base64Url writes no padding by default.
  • Size math. Encoded length is 4 * ceil(n / 3). Use Base64.GetMaxEncodedToUtf8Length or the same formula to size buffers.
  • Not secret. Base64 hides nothing. Never use it for passwords or tokens that must stay private; hash or encrypt instead.
  • URL-safe form. In Base64Url, + becomes -, / becomes _ and the padding is dropped. JWT tokens use it. Do not mix the two alphabets.
  • Line breaks. InsertLineBreaks inserts line breaks after every 76 characters (MIME style). Most APIs and JSON fields want a single line.
  • Large data. For streams, wrap a stream with CryptoStream and ToBase64Transform, or encode in chunks whose size is a multiple of 3 so that padding appears only at the very end.
  • Performance. The span overloads avoid allocating strings and arrays. System.Buffers.Text.Base64 works directly on UTF-8 bytes for the highest throughput.
  • Data URLs. For an inline image, the text goes after data:image/png;base64,.