Convert char to int in C#
Interesting answers but the docs say differently:
Use the
GetNumericValue
methods to
convert aChar
object that represents
a number to a numeric value type. Use
Parse
andTryParse
to convert a
character in a string into aChar
object. UseToString
to convert aChar
object to aString
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
How to Convert a C# String Value to an Escaped String Literal
Httpclient.Getasync(...) Never Returns When Using Await/Async
Double to String Conversion Without Scientific Notation
Simple Proof That Guid Is Not Unique
Why Is There No Foreach Extension Method on Ienumerable
Associate File Extension With Application
Is Floating-Point Math Consistent in C#? Can It Be
Easier Way to Debug a Windows Service
How to Run Servicestack on Linux/Mono
How to Migrate a .Net Windows Service Application to Linux Using Mono
Killing Gracefully a .Net Core Daemon Running on Linux
How to Compile .Net Core App for Linux on a Windows MAChine
Sockets on Same MAChine for Windows and Linux
Cross-Platform File Name Handling in .Net Core
How to Compile a Windows.Forms Application on Linux for .Net Core