Main Site Documentation

How to convert a hex string to a byte


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 ?


I’m using a Fez panda ;D


There are actually some bits of code on for this very thing. Take a look and you should find what you want. Here is one


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


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


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