I have a string
that contains numbers, like so:
string keyCode = "1200009990000000000990";
Now I want to get the number on position 2 as integer
, which I did like so:
int number = Convert.ToInt32(keyCode[1]);
But instead of getting 2
, I get 50
.
What am I doing wrong?
50 is the ascii code for char '2'. 0->48, 1->49 etc....
You can do
int number = keyCode[1]-'0';
You observed that when you do int n = Convert.ToInt32( '2' );
you get 50
. That's correct.
Apparently, you did not expect to get 50
, you expected to get 2
. That's what's not correct to expect.
It is not correct to expect that Convert.ToInt32( '2' )
will give you 2
, because then what would you expect if you did Convert.ToInt32( 'A' )
?
A character is not a number. But of course, inside the computer, everything is represented as a number. So, there is a mapping that tells us what number to use to represent each character. Unicode is such a mapping, ASCII is another mapping that you may have heard of. These mappings stipulate that the character '2'
corresponds to the number 50
, just as they stipulate that the character 'A'
corresponds to the number 65
.
Convert.ToInt32( char c )
performs a very rudimentary conversion, it essentially reinterprets the character as a number, so it allows you to see what number the character corresponds to. But if from '2'
you want to get 2
, that's not the conversion you want.
Instead, you want a more complex conversion, which is the following: int n = Int32.Parse( keyCode.SubString( 1, 1 ) );
Well, You got 50 because it is the ascii code of 2
.
You are getting it because you are pointing to a char
and when c# converts a char
to an int
it gives back its ascii code. You should use instead int.Parse
which takes a string
.
int.Parse(keyCode[1].ToString());
or
int.Parse(keyCode.Substring(1,1));
You need
int number = (int)Char.GetNumericValue(keyCode[1]);
The cast is needed because Char.GetNumericValue returns a double.
As Ofir said, another method is int number = int.Parse(keyCode[1].ToString())
.
This command can be explained like this: int
is shorthand for Int32
, which contains a Parse(string)
method. Parse
only works if the input is a string that contains only numbers, so it's not always the best to use unless TryParse
(the method that checks whether you can parse a string or not) has been invoked and returns true
, but in this case, since your input string is always a number, you can use Parse
without using TryParse
first. keyCode[1]
actually implicitly converts keyCode to a char[]
first so that it can retrieve a specific index, which is why you need to invoke ToString()
on it before you can parse it.
This is my personal favorite way to convert a string
or char
to an int
, since it's pretty easy to make sense of once you understand the conversions that are performed both explicitly and implicitly. If the string to convert isn't static, it may be better to use a different method, since an if
statement checking whether it can be parsed or a try
makes it take longer to code and execute than one of the other solutions.