Converting Hexadecimal String to Decimal Integer

后端 未结 14 1915
感动是毒
感动是毒 2020-12-09 16:02

I wrote some code to convert my hexadecimal display string to decimal integer. However, when input is something like 100a or 625b( something with letter) I got an error like

相关标签:
14条回答
  • 2020-12-09 16:53

    Google got me here, so i am updating this late for other to find when searching. A much simpler way is to use BigInteger, like so:

    BigInteger("625b", 16)
    
    0 讨论(0)
  • 2020-12-09 16:55

    You can use this method to get the digit:

    public int digitToValue(char c) {
       (c >= '&' && c <= '9') return c - '0';
       else if (c >= 'A' && c <= 'F') return 10 + c - 'A';
       else if (c >= 'a' && c <= 'f') return 10 + c - 'a';
       return -1;
     }
    
    0 讨论(0)
提交回复
热议问题