Convert Char to Int in C#

Convert char to int in C#

Interesting answers but the docs say differently:

Use the GetNumericValue methods to
convert a Char object that represents
a number to a numeric value type. Use
Parse and TryParse to convert a
character in a string into a Char
object. Use ToString to convert a Char
object to a String object.

http://msdn.microsoft.com/en-us/library/system.char.aspx

How to convert char to int?

I'm surprised nobody has mentioned the static method built right into System.Char...

int val = (int)Char.GetNumericValue('8');
// val == 8

Char to Int explicit Conversion in C#

You hit the overload methods of Conver.ToInt16(char) , and char is a UTF-16 code unit and representing 1 is 49. You can use Int16.TryParse to safe parse the number (need in 16 bit sign number) in order to get the valid integer.

               string str = "1";  //  use string if your number e.g 122
short number; // convert to 16 bit signed integer equivalent.
if (Int16.TryParse(str, out number))
{
Console.WriteLine(number);

}

Converting string or char to int

That is the ASCII value for the character 7 and 3. If you want number representation then you can convert each character to string and then use Convert.ToString:

string temp = "73";
int tempc0 = Convert.ToInt32(temp[0].ToString());
int tempc1 = Convert.ToInt32(temp[1].ToString());
MessageBox.Show(tempc0 + "*" + tempc1 + "=" + tempc0*tempc1);

Getting different values from convert char to integer

When you convert a char to an int, you get the ASCII value of that character. The neat thing with these values is that they are sequential, so if you subtract the value of '0' (the 0 character), you can convert a character representing a digit to that digit:

var hola =  i - '0';

Convert character to its alphabet integer position?

Programming 101:

char c = 'A';
//char c = 'b'; you may use lower case character.
int index = char.ToUpper(c) - 64;//index == 1

Char to int conversion to get ASCII

\01 is unprintable character, as result console is free to use any substitution to make it visible.

Also default Windows console is not very Unicode friendly, so strange things happen when you print characters outside of default printable ASCII range.

how to convert hex from char[] to int in c#

You can handle this base conversion using an ordered string of your hex characters and the IndexOf string method.

string hexabet = "0123456789ABCDEF";
char c = 'A';
int lastD = hexabet.IndexOf(c);
Console.WriteLine(lastD); // 10

Incorrect values when converting char digits to int

As mentioned the issue with your code is that characters have a ASCII code value when you cast to int which doesn't match with the various numerical digits. Instead of messing with strings and characters just use good old math instead.

public static int AddDigits(int n)
{
int total = 0;
while(n>0)
{
total += n % 10;
n /= 10;
}

return total;
}

Modulo by 10 will result in the least significant digit and because integer division truncates n /= 10 will truncate the least significant digit and eventually become 0 when you run out of digits.



Related Topics



Leave a reply



Submit