- Array
- How To Encode Strings and Decode Byte Arrays
How To Encode C# Strings and Decode Byte Arrays

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
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.
Letter | ASCII | Unicode | UTF32 | UTF7 | UTF8 |
---|---|---|---|---|---|
A | 65 | 65|0 | 65|0|0|0 | 65 | 65 |
B | 66 | 66|0 | 66|0|0|0 | 66 | 66 |
C | 67 | 67|0 | 67|0|0|0 | 67 | 67 |
D | 68 | 68|0 | 68|0|0|0 | 68 | 68 |
E | 69 | 69|0 | 69|0|0|0 | 69 | 69 |
F | 70 | 70|0 | 70|0|0|0 | 70 | 70 |
G | 71 | 71|0 | 71|0|0|0 | 71 | 71 |
H | 72 | 72|0 | 72|0|0|0 | 72 | 72 |
I | 73 | 73|0 | 73|0|0|0 | 73 | 73 |
J | 74 | 74|0 | 74|0|0|0 | 74 | 74 |
K | 75 | 75|0 | 75|0|0|0 | 75 | 75 |
L | 76 | 76|0 | 76|0|0|0 | 76 | 76 |
M | 77 | 77|0 | 77|0|0|0 | 77 | 77 |
N | 78 | 78|0 | 78|0|0|0 | 78 | 78 |
O | 79 | 79|0 | 79|0|0|0 | 79 | 79 |
P | 80 | 80|0 | 80|0|0|0 | 80 | 80 |
Q | 81 | 81|0 | 81|0|0|0 | 81 | 81 |
R | 82 | 82|0 | 82|0|0|0 | 82 | 82 |
S | 83 | 83|0 | 83|0|0|0 | 83 | 83 |
T | 84 | 84|0 | 84|0|0|0 | 84 | 84 |
U | 85 | 85|0 | 85|0|0|0 | 85 | 85 |
V | 86 | 86|0 | 86|0|0|0 | 86 | 86 |
W | 87 | 87|0 | 87|0|0|0 | 87 | 87 |
X | 88 | 88|0 | 88|0|0|0 | 88 | 88 |
Y | 89 | 89|0 | 89|0|0|0 | 89 | 89 |
Z | 90 | 90|0 | 90|0|0|0 | 90 | 90 |
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.