Base64 Encode Decode in C#

Published on 29 July 2025 (Updated: 13 May 2026)

Welcome to the Base64 Encode Decode in C# page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

using System.Text;

return args switch
{
    ["encode", var value] when !string.IsNullOrWhiteSpace(value)
        => Encode(value),

    ["decode", var value] when !string.IsNullOrWhiteSpace(value)
        => Decode(value),

    _ => Usage()
};

static int Encode(string value)
{
    Console.WriteLine(Convert.ToBase64String(Encoding.ASCII.GetBytes(value)));
    return 0;
}

static int Decode(string value)
{
    byte[] buffer = new byte[value.Length];
    if (!Convert.TryFromBase64String(value, buffer, out int written))
        return Usage();

    Console.WriteLine(Encoding.ASCII.GetString(buffer, 0, written));
    return 0;
}

static int Usage()
{
    Console.Error.WriteLine("Usage: please provide a mode and a string to encode/decode");
    return 1;
}

Base64 Encode Decode in C# was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.