简体   繁体   English

c# 中是否有类似 java 的 Character.digit(char ch, int radix) 之类的东西?

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

Character.digit(char ch, int radix)

Returns the numeric value of the character ch in the specified radix.返回指定基数中字符 ch 的数值。

Is there an equivalent function in c#? c#中是否有等效的函数?

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 转换为字符串,然后将其传递给上述函数以获取 int32 或 Int16 或 Byte,或者您想处理它:

char c = 'F';

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

Note - Convert will throw a FormatException if the char isn't a digit注意 - 如果字符不是数字,Convert 将抛出 FormatException

If you work with hex characters you can do:如果您使用十六进制字符,您可以执行以下操作:

if (System.Uri.IsHexDigit(c)) {
  int v = System.Uri.FromHex(c);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM