EN
C# / .NET - string to byte array
1
points
In C# / .NET it is possible to make string conversion to byte array in few ways.
1. UTF-8 encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.UTF8.GetBytes(text);
// test
string product = Encoding.UTF8.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text,-10} {product,-10} {status,-10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 £100 CORRECT
Bytes: 194 163 49 48 48
2. ASCII (7-bits) encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.ASCII.GetBytes(text);
// test
string product = Encoding.ASCII.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text, -10} {product, -10} {status, -10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 ?100 INCORRECT
Bytes: 63 49 48 48
Note: £ sign can not be encoded with ASCII because it is described by more than 7 bits.
3. UTF-7 encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.UTF7.GetBytes(text);
// test
string product = Encoding.UTF7.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text,-10} {product,-10} {status,-10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 £100 CORRECT
Bytes: 43 65 75 77 45 49 48 48
4. UTF-32 encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.UTF32.GetBytes(text);
// test
string product = Encoding.UTF32.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text,-10} {product,-10} {status,-10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 £100 CORRECT
Bytes: 163 0 0 0 49 0 0 0 48 0 0 0 48 0 0 0
5. Unicode (UTF-16) encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.Unicode.GetBytes(text);
// test
string product = Encoding.Unicode.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text,-10} {product,-10} {status,-10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 £100 CORRECT
Bytes: 163 0 49 0 48 0 48 0
6. BigEndianUnicode (UTF-16 BE) encoding example
string text = "£100";
// encoding (string to bytes conversion)
byte[] data = Encoding.BigEndianUnicode.GetBytes(text);
// test
string product = Encoding.BigEndianUnicode.GetString(data);
string status = (text == product ? "CORRECT" : "INCORRECT");
Console.WriteLine($"{"BEFORE",-10} {"AFTER",-10} {"STATUS",-10}");
Console.WriteLine($"{text,-10} {product,-10} {status,-10}");
Console.WriteLine();
// bytes
Console.Write("Bytes: ");
foreach (byte entry in data)
Console.Write(entry + " ");
Console.WriteLine();
Output:
BEFORE AFTER STATUS
£100 £100 CORRECT
Bytes: 0 163 0 49 0 48 0 48