C# – converting string (hex) to decimal results in exception


My question seems simple, but I have googled for a couple of hours without result.
I have to use a string (hex format) and convert to integer. (the string is returned from a commercial library).

if I do,

string stringInHex = getThevaluefromthelibrary(); //psuedo

and print stringInHex using


I can see 0x00

but if I try to convert it to a int, see below:

int myInt = int.Parse(stringInHex , System.Globalization.NumberStyles.HexNumber);

I get the following exception:

Exception errorSystem.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)

I think the string which is returned from the library that I am using is encoded weird in some how.

Best Solution

The string can not contain the 0x prefix. Simply passing the value 00 would achieve what you want.