Sometimes you need to parse hexadecimal strings to int values. I searched a while for the correct answer. You may use
Byte.Parse(string, NumberStyles.HexNumber);
As always, Parse will throw an exception (ArgumentNullException, FormatException and OverflowException), if the conversion fails. There are two options of NumberStyle you may use:
AllowHexSpecifier: Indicates that the numeric string represents a hexadecimal value. Valid hexadecimal values include the numeric digits 0-9 and the hexadecimal digits A-F and a-f. Hexadecimal values can be left-padded with zeros. Strings parsed using this style are not permitted to be prefixed with "0x".
HexNumber: Indicates that the AllowLeadingWhite, AllowTrailingWhite, and AllowHexSpecifier styles are used. This is a composite number style.


Letzte Kommentare