Main Site Documentation

How to convert a hex string to a byte


#1

Hi everybody. I need to convert a string that represents a hex value like this:
String MiHex = “7E”;
and then fill a byte[] like this:

Mibyte[0] = MiHex //Here is where i need to make the convertion

i have this function that runs well in windows form application:

public static byte String_ToByte(string hex)
{
try { return Enumerable.Range(0, hex.Length).Where(x => 0 == x % 2).Select(x => Convert.ToByte(hex.Substring(x, 2), 16)).ToArray()[0]; }
catch { return 0; }
}
Here is an example:
Mibyte[0] = String_ToByte(MiHex);

but in NETMF the Enumerable.Range is not included.
anyone know other way to do this, or how to fix the Enumerable.Range ?


#2

I’m using a Fez panda ;D


#3

There are actually some bits of code on Fezzer.com for this very thing. Take a look and you should find what you want. Here is one
http://www.fezzer.com/project/100/another-fast-hex-string-to-byte-conversion/


#4

;D i just used the code from the page and it’s great, that’s what i wanted.
Thanks.


#5

There is a lot of useful codes in fezzer.com. Just treat it as your library of codes.


#6

Another way to convert might be to use Value = Convert.ToInt32(strHEX, 16);