I have a string containing a hexadecimal value. Now I need the content of this string containing the hexadecimal as a byte variable. How should I do this without changing the hexadecimal value?
An alternative to the options posted so far:
byte b = Convert.ToByte(text, 16);
Note that this will return 0 if text is null; that may or may not be the result you want.
If it is just a single byte in the string, you can do this:
string s = "FF";
byte b;
if (byte.TryParse(s, NumberStyles.HexNumber, null, out b))
{
MessageBox.Show(b.ToString()); //255
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With