How To Encode C# Strings and Decode Byte Arrays

How To Encode Strings and Decode Byte Arrays Banner Image

Introduction

Encoding is the process of converting characters or strings into an array of bytes. Likewise decoding is the process of taking a byte array and converting it into a string which is a readable form for us. There are several encoding types. Unicode is the starting encoding type for strings. You can use different encoding types C#.

Encode and Decode

We can use the System.Text namespace to declare each of the encoding 5 encoding types. These enable both encoding the text into a byte array and decoding a byte array back into a string. Use the GetBytes for encoding the string and use GetString to decode the byte array.

Encode and Decode Example Code

This example code below takes a list of hobbies and encodes the text to a byte array and then decodes the same byte array. But you can use any text type.

using System.Text;

List<string> hobbyList = new List<string> {
            "Painting or Drawing",
            "Cooking or Baking",
        };

// Create instances of each encoding type
ASCIIEncoding asciiEncoding = new ASCIIEncoding();
UnicodeEncoding unicodeEncoding = new UnicodeEncoding();
UTF32Encoding utf32Encoding = new UTF32Encoding();
UTF7Encoding utf7Encoding = new UTF7Encoding();
UTF8Encoding utf8Encoding = new UTF8Encoding();

foreach (string hobby in hobbyList)
{
    Console.WriteLine("ASCIIEncoding");
    byte[] asciiEncodedBytes = PrintEncodedBytes(hobby, asciiEncoding);//Convert String to a byte array
    PrintDecodedBytes(asciiEncodedBytes, asciiEncoding);//Convert byte array to a string
    Console.WriteLine();

    Console.WriteLine("UnicodeEncoding");
    byte[] unicodeEncodedBytes = PrintEncodedBytes(hobby, unicodeEncoding);//Convert String to a byte array
    PrintDecodedBytes(unicodeEncodedBytes, unicodeEncoding);//Convert byte array to a string
    Console.WriteLine();

    Console.WriteLine("UTF32Encoding");
    byte[] utf32EncodedBytes = PrintEncodedBytes(hobby, utf32Encoding);//Convert String to a byte array
    PrintDecodedBytes(utf32EncodedBytes, utf32Encoding);//Convert byte array to a string
    Console.WriteLine();

    Console.WriteLine("UTF7Encoding");
    byte[] utf7EncodedBytes = PrintEncodedBytes(hobby, utf7Encoding);//Convert String to a byte array
    PrintDecodedBytes(utf7EncodedBytes, utf7Encoding);//Convert byte array to a string
    Console.WriteLine();

    Console.WriteLine("UTF8Encoding");
    byte[] utf8EncodedBytes = PrintEncodedBytes(hobby, utf8Encoding);//Convert String to a byte array
    PrintDecodedBytes(utf8EncodedBytes, utf8Encoding);//Convert byte array to a string
    Console.WriteLine();
    Console.WriteLine();
}


static byte[] PrintEncodedBytes(string text, Encoding encoding)
{
    byte[] encodedBytes = encoding.GetBytes(text);//Encode String value to a byte array
    Console.WriteLine($"{text} encoded into {string.Join(",", encodedBytes)}");//Print byte array
    return encodedBytes;
}

static string PrintDecodedBytes(byte[] encodedText, Encoding encoding)
{
    string decodedString = encoding.GetString(encodedText);//Encode byte array back into a string
    Console.WriteLine($"{string.Join(",", encodedText)} decoded into {decodedString}");
    return decodedString;
}
Code Output
UTF32Encoding
Painting or Drawing encoded into 80,0,0,0,97,0,0,0,105,0,0,0,110,0,0,0,116,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0,32,0,0,0,111,0,0,0,114,0,0,0,32,0,0,0,68,0,0,0,114,0,0,0,97,0,0,0,119,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0
80,0,0,0,97,0,0,0,105,0,0,0,110,0,0,0,116,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0,32,0,0,0,111,0,0,0,114,0,0,0,32,0,0,0,68,0,0,0,114,0,0,0,97,0,0,0,119,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0 decoded into Painting or Drawing

UTF7Encoding
Painting or Drawing encoded into 80,97,105,110,116,105,110,103,32,111,114,32,68,114,97,119,105,110,103
80,97,105,110,116,105,110,103,32,111,114,32,68,114,97,119,105,110,103 decoded into Painting or Drawing

UTF8Encoding
Painting or Drawing encoded into 80,97,105,110,116,105,110,103,32,111,114,32,68,114,97,119,105,110,103
80,97,105,110,116,105,110,103,32,111,114,32,68,114,97,119,105,110,103 decoded into Painting or Drawing

ASCIIEncoding
Cooking or Baking encoded into 67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103
67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103 decoded into Cooking or Baking

UnicodeEncoding
Cooking or Baking encoded into 67,0,111,0,111,0,107,0,105,0,110,0,103,0,32,0,111,0,114,0,32,0,66,0,97,0,107,0,105,0,110,0,103,0
67,0,111,0,111,0,107,0,105,0,110,0,103,0,32,0,111,0,114,0,32,0,66,0,97,0,107,0,105,0,110,0,103,0 decoded into Cooking or Baking

UTF32Encoding
Cooking or Baking encoded into 67,0,0,0,111,0,0,0,111,0,0,0,107,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0,32,0,0,0,111,0,0,0,114,0,0,0,32,0,0,0,66,0,0,0,97,0,0,0,107,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0
67,0,0,0,111,0,0,0,111,0,0,0,107,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0,32,0,0,0,111,0,0,0,114,0,0,0,32,0,0,0,66,0,0,0,97,0,0,0,107,0,0,0,105,0,0,0,110,0,0,0,103,0,0,0 decoded into Cooking or Baking

UTF7Encoding
Cooking or Baking encoded into 67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103
67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103 decoded into Cooking or Baking

UTF8Encoding
Cooking or Baking encoded into 67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103
67,111,111,107,105,110,103,32,111,114,32,66,97,107,105,110,103 decoded into Cooking or Baking
C# Programming for Unity Game Development Specialization

COURSERA

C# Programming for Unity Game Development Specialization

4.7 (2,230 reviews)

Beginner level

No previous experience is necessary

3-months at 10 hours a week (At your pace)

$59 / month or audit, and financial aid available

If you want to learn more about C# then check out this course on Coursera. It's a 3 month course to take you through the basics of C#.

You can get started with programming using C# and apply it to Unity games in this beginner-level program. Facilitated by the University of Colorado, the program offers a chance to grasp the basics of C# and utilize the knowledge to develop Unity games.

[Full Disclosure: As an affiliate, I receive compensation if you purchase through this link]

Start Your 7-day Free Trial

Encoding Types And Additional Info

Unicode Encoding stand for Unicode Transformation Format (UTF). It is supported by Unicode format standard

UTF32Encoding is in the form of a 32-bit integer and uses big-endian order.

UTF8Encoding is in a sequence of one to four bytes.

ASCIIEncoding does not provide error detection. It is also a 7-bit encoding.

Summary Table

The below summary table gives an idea of what each encoding type looks like so if you don't know which your code is encoding this should help.

LetterASCIIUnicodeUTF32UTF7UTF8
A6565|065|0|0|06565
B6666|066|0|0|06666
C6767|067|0|0|06767
D6868|068|0|0|06868
E6969|069|0|0|06969
F7070|070|0|0|07070
G7171|071|0|0|07171
H7272|072|0|0|07272
I7373|073|0|0|07373
J7474|074|0|0|07474
K7575|075|0|0|07575
L7676|076|0|0|07676
M7777|077|0|0|07777
N7878|078|0|0|07878
O7979|079|0|0|07979
P8080|080|0|0|08080
Q8181|081|0|0|08181
R8282|082|0|0|08282
S8383|083|0|0|08383
T8484|084|0|0|08484
U8585|085|0|0|08585
V8686|086|0|0|08686
W8787|087|0|0|08787
X8888|088|0|0|08888
Y8989|089|0|0|08989
Z9090|090|0|0|09090

C# provides these methods to quickly encode and decode the text. There is a default encoding type but Microsoft has advise not to use this as it can give inconsistent results.

Know any other ways to encode and decode text? Let me know in the comments below.

Get Latest Updates