Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

C# / .NET - string to byte array

0 contributions
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

References

  1. Encoding Class - Microsoft Docs
  2. Encoding.GetBytes - Microsoft Docs
0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey 👋
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more