Using Base64 To Encode Binary Data
[C#, .NET]
You would be surprised how much computing is powered by plain old text, particularly ASCII. Take, for example, the humble email.
“Even email with images and attachments?” you might ask?
Yes.
Any binary data is encoded into text, using a protocol called Base64.
Base64
is named after the fact that it uses a set of 64 printable characters to encode data.
- Uppercase letters:
A
-Z
(26) - Lowercase letters:
a
-z
(26) - Digits:
0
-9
(10) - Symbols:
+
/
(2)
Base64
also has padding, =
. This is used to make the encoded output a multiple of 4 characters.
This is natively supported in .NET
using Serilog;
using System.Text;
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
// First get the text into a byte array
var textToEncode = "Hello";
// Encode the text
var rawBytes = Encoding.UTF8.GetBytes(textToEncode);
// Print to console
Log.Information("The encoded representation of {Original} is {Current}", textToEncode,
Convert.ToBase64String(rawBytes));
This program will print the following:
Base64
conversion is handled using the Convert.ToBase64String method.
Note the padding used to make the final string 8 characters.
You can also leverage the fact that the compiler can do UTF8 encoding for you as follows:
// Get a ReadOnlySpan<byte> of the text
var rawBytes = "Hello"u8;
// Print to console
Log.Information("The encoded representation of {Original} is {Current}", "Hello",
Convert.ToBase64String(rawBytes));
This is useful for representing binary data under several scenarios:
- JSON & XML
- Encoding in CSS & HTML
Occasionally, you need to pass encoded data using a URL. This presents a number of problems:
/
means something in a URL+
means something in a URL=
means something in a URL
This means you will have to do some extra work to remove them for a URL.
Luckily there exists a method that does this heavy lifting for you - Base64Url.EncodeToString
Using the example above:
// Print to console
Log.Information("The encoded representation of {Original} is {Current}", "Hello",
Convert.ToBase64String(rawBytes));
Log.Information("The url encoded representation of {Original} is {Current}", "Hello",
Base64Url.EncodeToString(rawBytes));
This will print the following:
Note the padding has been omitted.
TLDR
Base64
encoding is a useful technique to represent binary data as printable text. However, care needs to be taken if you want to pass around Base64
encoded data in a URL.
The code is in my GitHub.
Happy hacking!