How to I assign some bytes in hex to a byte array

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

If I have some bytes that are represented in hex like this.
12, bd,8f,7e,4e,4d. How do I assign all these bytes to a byte array ?
If the bytes were represented in decimal I would have been able to use this
statement
byte[] myBytes = new byte[]{ here I could have specified all the bytes with
comma in between };

But now when I have hex it's not possible. I could translate the hax value
into decimal but I hope there must be a better way.

//Tony
 
If I have some bytes that are represented in hex like this.
12, bd,8f,7e,4e,4d. How do I assign all these bytes to a byte array ?
If the bytes were represented in decimal I would have been able to use
this statement
byte[] myBytes = new byte[]{ here I could have specified all the bytes
with comma in between };

But now when I have hex it's not possible. I could translate the hax value
into decimal but I hope there must be a better way.

Define "represented in hex." Do you mean you have a STRING containing hex
digits? If so, what does the string look like?
 
Jeff Johnson said:
If I have some bytes that are represented in hex like this.
12, bd,8f,7e,4e,4d. How do I assign all these bytes to a byte array ?
If the bytes were represented in decimal I would have been able to use
this statement
byte[] myBytes = new byte[]{ here I could have specified all the bytes
with comma in between };

But now when I have hex it's not possible. I could translate the hax
value into decimal but I hope there must be a better way.

Define "represented in hex." Do you mean you have a STRING containing hex
digits? If so, what does the string look like?

The string looks like this "12,bd,8f,7e,4e,4d" where each item in this
string 12 and db and 8f and so on should be converted to a byte and stored
in a byte array.

//Tony
 
The string looks like this "12,bd,8f,7e,4e,4d" where each item in this
string 12 and db and 8f and so on should be converted to a byte and stored
in a byte array.

What solutions have you tried sofar?
Maybe if you post your failed attempts, we can help you solve it.
 
The string looks like this "12,bd,8f,7e,4e,4d" where each item in this
string 12 and db and 8f and so on should be converted to a byte and stored
in a byte array.

Then you need to first use the Split() method with a comma as the delimiter
and then run each member of the resulting array through the byte.Parse()
method to get a real byte back. Use the overload that takes a numeric format
and specify <name of enum which I don't remember>.HexNumber.
 
If I have some bytes that are represented in hex like this.
12, bd,8f,7e,4e,4d. How do I assign all these bytes to a byte array ?
If the bytes were represented in decimal I would have been able to use this
statement
byte[] myBytes = new byte[]{ here I could have specified all the bytes with
comma in between };

But now when I have hex it's not possible. I could translate the hax value
into decimal but I hope there must be a better way.

There are several ways.

One of the nice is:

public static byte[] FromHex(string s)
{
byte[] ba = new byte[(s.Length+1)/3];
for(int i = 0; i < ba.Length; i++)
{
ba = byte.Parse(s.Substring(3 * i, 2),
NumberStyles.HexNumber);
}
return ba;
}

byte[] b = FromHex(s);

One of the short is:

byte[] b = s.Split(',').Select((s) => byte.Parse(s,
NumberStyles.HexNumber)).ToArray();

Arne
 
Back
Top