Convert.ToInt32(char)

  • Thread starter Thread starter omeraa
  • Start date Start date
O

omeraa

If we pass in '9' the above function should return 9, but it doesnt. It
returns 57, which is the unicode representation of the character 9.

However if we use Convert.Int32(string), and pass in "9", it returns 9.

Any ideas why?
 
If we pass in '9' the above function should return 9, but it doesnt. It
returns 57, which is the unicode representation of the character 9.

What makes you think it should return 9?
However if we use Convert.Int32(string), and pass in "9", it returns 9.

Any ideas why?

Because they both act as documented. From Convert.ToInt32(char):

<quote>
Converts the value of the specified Unicode character to the equivalent
32-bit signed integer.
</quote>

From Convert.ToInt32(string):
<quote>
Converts the specified String representation of a number to an
equivalent 32-bit signed integer.
</quote>

Note the difference between the string (i.e. textual) *representation*
of a number and an actual *value*. (Any char is in itself a 16 bit
unsigned value.)
 
Back
Top