Character.digit(char ch, int radix)
Returns the numeric value of the character ch in the specified radix.
Is there an equivalent function in c#?
Character.digit(char ch, int radix)
Returns the numeric value of the character ch in the specified radix.
Is there an equivalent function in c#?
No, as of version 3.5, there's no equivalent in the .NET Framework BCL. It's pretty easy to write though.
static int GetIntegerValue(char c, int radix)
{
int val = -1;
if (char.IsDigit(c))
val = (int)(c - '0');
else if (char.IsLower(c))
val = (int)(c - 'a') + 10;
else if (char.IsUpper(c))
val = (int)(c - 'A') + 10;
if (val >= radix)
val = -1;
return val;
}
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
bool isDigit = char.IsDigit('a');
This only does numbers 0-9 however, so for hex you would have to add code to see if your char was between a and f.
char.IsNumber will handle more numeric characters but may not be what you want.
See this post for making an extension method to handle this for you: http://stackoverflow.com/questions/228523/char-ishex-in-c