Is there something like java's Character.digit(char ch, int radix) in c#?

后端 未结 2 410
北恋
北恋 2021-01-19 18:00
Character.digit(char ch, int radix)

Returns the numeric value of the character ch in the specified radix.

Is there an equivalent function i

相关标签:
2条回答
  • 2021-01-19 18:27

    I don't know of a direct equivalent
    The closest match I can find is

    Convert.ToInt32(string s, int baseFrom);  
    

    So you could convert your char to string then pass it in to the above function to get the int32 or Int16 or Byte or however you want to handle it :

    char c = 'F';
    
    int digit = Convert.ToInt32(c.ToString(),16);
    

    Note - Convert will throw a FormatException if the char isn't a digit

    0 讨论(0)
  • 2021-01-19 18:32

    If you work with hex characters you can do:

    if (System.Uri.IsHexDigit(c)) {
      int v = System.Uri.FromHex(c);
    }
    
    0 讨论(0)
提交回复
热议问题