Languages
[Edit]
EN

C# / .NET - string to byte array

1 points
Created by:
Pearl-Hurley
559

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

References

  1. Encoding Class - Microsoft Docs
  2. Encoding.GetBytes - Microsoft Docs
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join