Languages
[Edit]
EN

C# / .NET - string to byte array

1 points
Created by:
JustMike
3485

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

Checkout latest findings & news:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Rockstar community members
Thank you for great work 👍