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

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
 
J

Jeff Johnson

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?
 
T

Tony Johansson

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
 
W

Willem van Rumpt

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.
 
J

Jeff Johnson

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.
 
A

Arne Vajhøj

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top