When working with text, you may probably need to work with Encodings, that represent the persistence and loading of text from files, memory, network and other streams.

You will almost always be dealing with UTF8, that is the ubiquitous encoding in use across operating systems.

For example, to get the byte array representation of text you would do it like this:

byte[] utf = Encoding.UTF8.GetBytes("This is some text with an emoji 🤗");

Here you can see I am explicitly requesting UTF8.

There are other encodings that you may occasionally need to use directly, like ASCII.

byte[] ascii = Encoding.ASCII.GetBytes("This is some text with an emoji 🤗");

In some previous posts, we have worked with ASCII to do Base64 encoding.

At which point we can ask ourselves - what is the difference?

We can check if these two are the same like this:

// Check if they are equal 
Console.WriteLine(utf.SequenceEqual(ascii));

This will print false.

encodingNoMatch

Why?

We can see the reason if we try and retrieve the string from the encoded bytes.

decodedStrings

We can see here that the ASCII encoding is unable to preserve the emoji. This is because the emoji (being 32 bit) is not representable in ASCII (which is 7 bit).

This is why the encoding when dealing with data matters.

You will find, in some examples code, logic like this:

byte[] utf = Encoding.Default.GetBytes("This is some text with an emoji 🤗");

What does this Encoding.Default refer to?

In .NET Core, and later versions, this is always UTF8. In .NET Framework (Windows Only!), it would return the encoding that corresponds to the system’s active code page.

You can check for yourself like so:

Console.WriteLine(Encoding.Default.EncodingName);

This will return the following:

defaultEncoding

TLDR

Encoding.Default is generally synonomous with Encoding.UTF8

The code is in my GitHub.

Happy hacking!