UInt32.TryParse() hex-number not working

后端 未结 4 1399
滥情空心
滥情空心 2020-12-10 11:17

For some reason the following C# Console program always outputs:

32
False
wtf=0

What am I doing wrong?



        
相关标签:
4条回答
  • 2020-12-10 11:45

    See also http://msdn.microsoft.com/en-us/library/kadka85s%28v=VS.100%29.aspx In the example at the bottom of the page:

    Attempted conversion of '0x8F8C' failed.

    0 讨论(0)
  • 2020-12-10 11:46

    You need to drop the "0x" prefix. Please see this blog entry

    0 讨论(0)
  • 2020-12-10 11:57

    Get rid of the leading "0x" in the string you're trying to parse.

    0 讨论(0)
  • 2020-12-10 12:09
    // stupid but effective way to improve the parsing
    char[] _trim_hex = new char[] {'0','x'};
    int temp;
    
    if (int.TryParse(value.TrimStart(_trim_hex), NumberStyles.HexNumber, null, out temp))
    {
        // temp is good
    }
    
    0 讨论(0)
提交回复
热议问题