The diary of a .NET Developer, working on an unlaunched startup project. Also, I live and work in Southern Oregon.

Wednesday, February 4, 2009

Char to Int vs String to Int

I spent way to much time staring at this code trying to figure this out:

int number = 10;
int firstDigit = Convert.ToInt32(number.ToString()[0]);
Assert.AreEqual(1,firstDigit) //fails: firstDigit == 49

I knew it was returning the Unicode value of 1, which is 49, but I was after the integer value.  Converting a char to an int always returns the Unicode value, not the integer value.  Converting a string returns the expected result.

int number = 10;
int firstDigit = Convert.ToInt32(number.ToString()[0].ToString()); //add an extra ToString()
Assert.AreEqual(1,firstDigit) //passes

Nothing too exciting, but I was stumped for about 15 minutes trying to figure this out.  I almost started looking for a atoi() function.


No comments:

Post a Comment