Search code examples
c#int32

Converting something like 0x0EB8 from a string to an int32 type?


I need to take a vendor ID from a user in the form of an int32, and they usually look something like this. 0x0EB8 I can write this as code

    int32 vid = 0x0EB8;

That works just fine. But I need to get it from the user in the form of a string. And when I call System.Convert.ToInt32("0x0EB8") I get a Type Conversion Exception.

here is some of my test code that gives me the exception.

        Int32 blah;
        Console.WriteLine("Please enter the Vendor ID");
        string blahString = Console.ReadLine();
        blah = Convert.ToInt32(blahString);

Anyone know a good way to do this??


Solution

  • You can pass in the radix as second parameter.

    int blah = Convert.ToInt32(blahString, 16);