How to convert a string containing a hexadecimal pair to byte?

I have a string containing a hexadecimal value. Now I need the contents of this line containing the hexadecimal number as a byte variable. How can I do this without changing the hex value?

+3
source share
3 answers

alternative to parameters published so far:

byte b = Convert.ToByte(text, 16);

Note that this will return 0 if textnull; which may or may not be the result you want.

+6
source
String strHex = "ABCDEF";
Int32 nHex = Int32.Parse(strHex, NumberStyles.HexNumber);
Byte[] bHex = BitConverter.GetBytes(nHex);

I think what you are looking for. If not, post an update with a more explicit definition of what you are looking for.

+2
source

, :

        string s = "FF";
        byte b;


        if (byte.TryParse(s, NumberStyles.HexNumber, null, out b))
        {
            MessageBox.Show(b.ToString());  //255
        }
0

Source: https://habr.com/ru/post/1791855/


All Articles