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?

Marvin Klein
  • 1,436
  • 10
  • 33
  • 2
    Does this answer your question? [Converting string to byte array in C#](https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp) – Self Jan 07 '21 at 15:05
  • 48 decimal == 0x30 hexadecimal. 54 == 0x36 and 50 == 0x32. You might also want to look at: https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa – Flydog57 Jan 07 '21 at 15:11
  • @Flydog57, isn't that the perfect dupe? – Drag and Drop Jan 07 '21 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 – Flydog57 Jan 07 '21 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 – Marvin Klein Jan 07 '21 at 15:29
  • Then your code works. Just look at the values of the array in hex, not decimal. – Flydog57 Jan 07 '21 at 15:45
  • It makes no sense to talk about a "hex byte array" but not a string. Byte arrays contain bytes, which are numbers. Numbers can be represented in an infinite number of bases. The way you see those representations is via a string. You see a string in the debug visualizer. You see a string when you output the variable via `Console.Write()`. – Heretic Monkey Jan 07 '21 at 15:47
  • @Flydog57 how do I switch the mode from decimal to hex in Visual Studio debugger? – Marvin Klein Jan 07 '21 at 15:52
  • You can see a single value in hexadecimal in the debugger by appending `, h` to the end of the expression to evaluate. For example, if you have a byte array `myArray`, and you want to see the value of first byte in hex, look at `myArray[0], h`. You could also write a Data Visualizer for VS and plug it in (I did it once, I remember it as being pretty easy): https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-visualizers-of-data?view=vs-2019 – Flydog57 Jan 07 '21 at 16:37

2 Answers2

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

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • 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 – Marvin Klein Jan 07 '21 at 15:20
  • @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. – Jamiec Jan 07 '21 at 18:03
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")));
Peter B
  • 22,460
  • 5
  • 32
  • 69