C# Base64
Last modified: July 16, 2026
This C# Base64 tutorial shows how to encode and decode binary data to and from
Base64 using the Convert class and the
System.Buffers.Text.Base64 API. We cover basic encoding and
decoding, URL-safe encoding, span-based encoding, image encoding, and extension
methods.
Base64
Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format by translating it into a radix-64 representation. It is defined in RFC 4648. Each Base64 digit represents exactly six bits of data, so three bytes (24 bits) can be represented using four Base64 characters.
Base64 encoding schemes are commonly used when we need to store and transfer binary data over media that are designed to deal with text. Common use cases include embedding images in HTML and CSS files, encoding email attachments via MIME, storing binary data in JSON or XML, and transmitting credentials in HTTP headers (Basic authentication).
In .NET, we can use the Convert class for straightforward
Base64 conversion and the System.Buffers.Text.Base64 class for
higher-performance, span-based encoding and decoding.
The following is the Base64 alphabet as defined by RFC 4648. Each character represents a six-bit value, ranging from 0 to 63.
Value Encoding Value Encoding Value Encoding Value Encoding 0 A 17 R 34 i 51 z 1 B 18 S 35 j 52 0 2 C 19 T 36 k 53 1 3 D 20 U 37 l 54 2 4 E 21 V 38 m 55 3 5 F 22 W 39 n 56 4 6 G 23 X 40 o 57 5 7 H 24 Y 41 p 58 6 8 I 25 Z 42 q 59 7 9 J 26 a 43 r 60 8 10 K 27 b 44 s 61 9 11 L 28 c 45 t 62 + 12 M 29 d 46 u 63 / 13 N 30 e 47 v 14 O 31 f 48 w (pad) = 15 P 32 g 49 x 16 Q 33 h 50 y
The Convert class provides the primary API for Base64
conversion in .NET. The static ToBase64String and
FromBase64String methods handle the common case of converting
between byte arrays and Base64 strings.
| Method | Description |
|---|---|
Convert.ToBase64String |
Converts a byte array to a Base64 string. |
Convert.FromBase64String |
Converts a Base64 string to a byte array. |
Base64.EncodeToUtf8 |
Encodes span of bytes to UTF-8 encoded Base64. |
Base64.DecodeFromUtf8 |
Decodes UTF-8 encoded Base64 span to bytes. |
For applications that need higher throughput or work with
Span<byte>, the System.Buffers.Text.Base64 class
offers the EncodeToUtf8 and DecodeFromUtf8 methods,
which operate directly on byte spans and avoid intermediate string allocations.
Convert.ToBase64String
The Convert.ToBase64String method converts an array of 8-bit
unsigned integers to its equivalent string representation that is encoded with
Base64 digits.
using System.Text;
string msg = "one 🐘 and three 🐋";
byte[] data = Encoding.UTF8.GetBytes(msg);
string base64 = Convert.ToBase64String(data);
Console.WriteLine(msg);
Console.WriteLine(string.Join(' ', data.Select(e => e.ToString("X2"))));
Console.WriteLine(base64);
The example encodes a string containing emoji characters to Base64.
byte[] data = Encoding.UTF8.GetBytes(msg);
First, we transform the string to a UTF-8 byte array.
string base64 = Convert.ToBase64String(data);
Then we convert the byte array into a Base64 string with
Convert.ToBase64String.
$ dotnet run one 🐘 and three 🐋 6F 6E 65 20 F0 9F 90 98 20 61 6E 64 20 74 68 72 65 65 20 F0 9F 90 8B b25lIPCfkJggYW5kIHRocmVlIPCfkIs=
Convert.FromBase64String
The Convert.FromBase64String converts a Base64 string to an
equivalent 8-bit unsigned integer array.
using System.Text;
string base64 = "b25lIPCfkJggYW5kIHRocmVlIPCfkIs=";
byte[] data = Convert.FromBase64String(base64);
string msg = Encoding.UTF8.GetString(data);
Console.WriteLine(base64);
Console.WriteLine(string.Join(' ', data.Select(e => e.ToString("X2"))));
Console.WriteLine(msg);
We convert a Base64 string back to its original form with
Convert.FromBase64String.
$ dotnet run b25lIPCfkJggYW5kIHRocmVlIPCfkIs= 6F 6E 65 20 F0 9F 90 98 20 61 6E 64 20 74 68 72 65 65 20 F0 9F 90 8B one 🐘 and three 🐋
URL-safe encoding
The standard Base64 alphabet includes + and /
characters, which have special meaning in URL path segments and query strings.
.NET does not provide a built-in URL-safe encoder, but we can easily produce
URL-safe output by replacing + with - and /
with _, and removing padding.
using System.Text;
string msg = "<<Hello>>";
byte[] data = Encoding.UTF8.GetBytes(msg);
string standard = Convert.ToBase64String(data);
Console.WriteLine("Standard: " + standard);
string urlSafe = standard
.Replace('+', '-')
.Replace('/', '_')
.TrimEnd('=');
Console.WriteLine("URL-safe: " + urlSafe);
// Decode URL-safe Base64
string padded = urlSafe
.Replace('-', '+')
.Replace('_', '/')
.PadRight((urlSafe.Length + 3) / 4 * 4, '=');
byte[] decoded = Convert.FromBase64String(padded);
Console.WriteLine("Decoded: " + Encoding.UTF8.GetString(decoded));
The example encodes <<Hello>> to standard Base64,
then converts it to a URL-safe variant by replacing + with
- and removing padding. The decoding process reverses these
transformations.
$ dotnet run Standard: PDxIZWxsbz4+ URL-safe: PDxIZWxsbz4- Decoded: <<Hello>>
Encoding images
A common practical use of Base64 is embedding binary image data directly into HTML or CSS files. The following example reads an image file and encodes it.
byte[] imageBytes = File.ReadAllBytes("image.png");
string base64 = Convert.ToBase64String(imageBytes);
Console.WriteLine("data:image/png;base64," + base64);
The example reads a PNG image into a byte array with
File.ReadAllBytes and encodes it with
Convert.ToBase64String, producing a data URI suitable for
embedding in an <img> tag.
Span-based encoding
The System.Buffers.Text.Base64 class provides span-based
encoding and decoding for high-performance scenarios. It operates directly on
Span<byte> values containing UTF-8 text, avoiding string
allocations.
using System.Buffers.Text; using System.Text; string msg = "one 🐘 and three 🐋"; byte[] src = Encoding.UTF8.GetBytes(msg); // Encode to UTF-8 Base64 span byte[] encoded = new byte[Base64.GetMaxEncodedToUtf8Length(src.Length)]; Base64.EncodeToUtf8(src, encoded, out _, out int bytesWritten); Console.WriteLine(Encoding.UTF8.GetString(encoded, 0, bytesWritten)); // Decode from UTF-8 Base64 span byte[] decoded = new byte[Base64.GetMaxDecodedFromUtf8Length(encoded.Length)]; Base64.DecodeFromUtf8(encoded.AsSpan(0, bytesWritten), decoded, out _, out bytesWritten); Console.WriteLine(Encoding.UTF8.GetString(decoded, 0, bytesWritten));
The example uses Base64.EncodeToUtf8 and
Base64.DecodeFromUtf8 to encode and decode data using spans
rather than string objects.
byte[] encoded = new byte[Base64.GetMaxEncodedToUtf8Length(src.Length)]; Base64.EncodeToUtf8(src, encoded, out _, out int bytesWritten);
GetMaxEncodedToUtf8Length pre-calculates the required buffer size.
EncodeToUtf8 encodes the source span into the destination span
and reports the number of bytes written.
$ dotnet run b25lIPCfkJggYW5kIHRocmVlIPCfkIs= one 🐘 and three 🐋
Extension methods
In the following example, we create extension methods for convenient Base64 encoding and decoding of strings.
using System.Text;
namespace Base64Ex;
class Program
{
static void Main()
{
string msg = "one 🐘 and three 🐋";
string base64 = msg.EncodeBase64();
string msg2 = base64.DecodeBase64();
Console.WriteLine(msg);
Console.WriteLine(base64);
Console.WriteLine(msg2);
}
}
static class ExtensionMethods
{
public static string EncodeBase64(this string value)
{
byte[] data = Encoding.UTF8.GetBytes(value);
return Convert.ToBase64String(data);
}
public static string DecodeBase64(this string value)
{
byte[] data = Convert.FromBase64String(value);
return Encoding.UTF8.GetString(data);
}
}
The program creates the EncodeBase64 and
DecodeBase64 extension methods that can be directly called on
string instances.
$ dotnet run one 🐘 and three 🐋 b25lIPCfkJggYW5kIHRocmVlIPCfkIs= one 🐘 and three 🐋
Source
System.Buffers.Text.Base64 - language reference
In this article we have shown how to encode and decode binary data to and from Base64 in C#.
Author
List all C# tutorials.