Converting "0x..." string to an integer RRS feed

  • Question

  • Hello,


    When trying to do the following code snippet:


    string s = "0x0001";

    int i = Int32.Parse(s, NumberStyles.HexNumber);


    Int32.Parse failes to convert a string starting with 0x.

    Does anyone know a way to do it?


    Obviously it can be done manually - check a string prefix (== 0x) and removing it, but I am searching for an .NET

    built-in solution.



    Wednesday, September 5, 2007 5:05 AM


All replies


    Removing it manually is indeed the way to go. 0x is a prefix specific to C-style programming languages, and the .NET framework doesn't provide any more support for that than for any other language syntax.
    Wednesday, September 5, 2007 7:39 AM
  • This works:
      int i = Convert.ToInt32("0x0001", 16);
    Wednesday, September 5, 2007 1:55 PM