0

I need to cast a string to a byte array in hex representation. For example:

Value: 06000002 What I need is:

30 36 30 30 30 30 30 32

I tried to implicit convert all chars to byte as following:

byte[] bytes = new byte[daten.Length];
for (int i = 0; i < daten.Length; i++)
{
    int value = Convert.ToInt32(daten[i]);
    
    bytes[i] = (byte)daten[i];
}

However I always get this result:

48 54 48 48 48 48 48 50

I do not need the result as string! I need it as byte array!

How do achive this?

9
  • 2
    Does this answer your question? Converting string to byte array in C# Commented Jan 7, 2021 at 15:05
  • 48 decimal == 0x30 hexadecimal. 54 == 0x36 and 50 == 0x32. You might also want to look at: stackoverflow.com/questions/311165/… Commented Jan 7, 2021 at 15:11
  • @Flydog57, isn't that the perfect dupe? Commented Jan 7, 2021 at 15:14
  • I marked it as a dupe. However, the conversion from a string to a byte array makes it not quite dup-y. Note that PeterB has an answer below that assumes a character fits in a byte (that will never fail). Jamiec is assuming UTF-8 encoding. @Marvin, how are you planning to convert that string into a byte array (I notice that you have a variable named daten, which I'm guessing is German. A ß fits in a byte (it's a U+00DF), but just. Lots of characters will not Commented Jan 7, 2021 at 15:25
  • @Flydog57 I need to cast a string (only numbers, length 8) into a byte array where the byte represents the value of the char as ascii hex value. Daten is the variable for which contains the string Commented Jan 7, 2021 at 15:29

2 Answers 2

6

All you should need is:

var value = "06000002";
byte[] bytes = Encoding.UTF8.GetBytes(value);

In .NET 5 you can then use

string hexString = Convert.ToHexString(bytes);

To verify your result is what you expected

3036303030303032

https://dotnetfiddle.net/6sUmgE

Sign up to request clarification or add additional context in comments.

2 Comments

As mentioned in my question above, I do not need it as string. I need the byte value itself representing the hex value. When I call the GetBytes method I get the bytes as dec value instead of hex
@MarvinKlein I'm sorry but that does not make sense. Bytes are bytes. There's no such thing as a "Hex byte" or a "dec byte". When you represent that data you can do so as base 10 (dec) or base 16 (hex) or whatever other numbering system you like.
0

To get the desired output, you can convert each character to its hex representation:

var s = "06000002";
var bytes = s.Select(c => (byte) c);
var hexCodes = bytes.Select(b => b.ToString("X2"));

You could stop here, or perhaps convert it to an Array or List.

Note that bytes are just numbers, there is no such thing as "hex bytes". The only time where "hex" exists is after conversion to string format, as I did above.

To still get it as one string you can proceed with this:

var result = string.Join(' ', hexCodes);

Or all in one go:

var result = string.Join(' ', "06000002".Select(c => ((byte) c).ToString("X2")));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.