Development

Working with Base64 in C# and .NET

Learn how to encode and decode Base64 in C# using Convert.ToBase64String and handle byte arrays safely in .NET applications.

Published on August 18, 20265 min read

In C# and the .NET ecosystem, Base64 conversion is built directly into the standard System.Convert class. Because .NET is a strongly-typed framework, you'll always be converting explicitly between byte[] arrays and string representations.

Text Encoding Matters

Before encoding a string to Base64 in C#, you must extract its underlying bytes. The class System.Text.Encoding.UTF8 is standard, but you can also use ASCII or Unicode depending on your specific requirements.

1. Encode a String to Base64 in C#

To encode a string, you first retrieve the byte array using Encoding.UTF8.GetBytes(), then pass that array to Convert.ToBase64String().

csharp
using System;
using System.Text;

class Program
{
    static void Main()
    {
        string text = "Hello, C# Base64!";
        
        // 1. Convert string to byte array
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        
        // 2. Convert byte array to Base64 string
        string base64String = Convert.ToBase64String(bytes);
        
        Console.WriteLine(base64String);
        // Output: SGVsbG8sIEMjIEJhc2U2NCE=
    }
}

2. Decode a Base64 String in C#

To reverse the process, use Convert.FromBase64String() to get the original byte array back, then convert those bytes back into a string.

csharp
using System;
using System.Text;

class Program
{
    static void Main()
    {
        string base64String = "SGVsbG8sIEMjIEJhc2U2NCE=";
        
        try 
        {
            // 1. Convert Base64 string to byte array
            byte[] bytes = Convert.FromBase64String(base64String);
            
            // 2. Convert byte array back to string
            string decodedText = Encoding.UTF8.GetString(bytes);
            
            Console.WriteLine(decodedText);
            // Output: Hello, C# Base64!
        }
        catch (FormatException)
        {
            Console.WriteLine("The string is not a valid Base64 format.");
        }
    }
}

3. Base64URL Encoding in C#

Standard Base64 contains + and /, which break URLs. .NET does not have a built-in ToBase64UrlString method in the standard Convert class, but Microsoft provides it in the Microsoft.AspNetCore.WebUtilities package via WebEncoders.Base64UrlEncode.

If you don't want to import a package, you can easily implement the URL-safe substitution yourself:

csharp
using System;
using System.Text;

public class Base64UrlConverter
{
    public static string Encode(string text)
    {
        byte[] bytes = Encoding.UTF8.GetBytes(text);
        string base64 = Convert.ToBase64String(bytes);
        
        // Replace URL-unsafe characters
        base64 = base64.Replace("+", "-").Replace("/", "_");
        
        // Strip padding
        base64 = base64.TrimEnd('=');
        
        return base64;
    }

    public static string Decode(string base64Url)
    {
        // Re-add URL-unsafe characters
        string base64 = base64Url.Replace("-", "+").Replace("_", "/");
        
        // Pad with '=' so the length is a multiple of 4
        switch (base64.Length % 4)
        {
            case 2: base64 += "=="; break;
            case 3: base64 += "="; break;
        }
        
        byte[] bytes = Convert.FromBase64String(base64);
        return Encoding.UTF8.GetString(bytes);
    }
}

Need to convert files or URL-safe strings?

Use our comprehensive toolkit to instantly encode and decode Base64 in multiple formats directly from your browser.

Go to the Base64 Converter