CallumVass
CallumVass

Reputation: 11448

Casting chars to integers gives unexpected values

I have the following code:

var stringNumber = 93.ToString(CultureInfo.InvariantCulture);
var stringArray = stringNumber.ToCharArray();
var intArray = stringArray.Select(x => (int)x).ToArray();

What I would expect intArray to contain are the values 9 and 3 but instead it contains 57 and 51 and I'm not sure why? How would I get intArray to contain the expected values?

Upvotes: 2

Views: 148

Answers (8)

stackh34p
stackh34p

Reputation: 8999

Try this:

const int offset = (int) '0';
var intArray = stringArray.Select(x => ((int) x) - offset).ToArray();

The problem with your code is that char values are internally represented as integers. However, the integer value is the code of the character, not the value of that character converted to integer. For instance the code of the character 0 is the integer 48, according to the ASCII character table.

The above code takes the value of the character '0' and subtracts it from the character value of your array. This works, because in ASCII digits are stored sequentially - the digits from 0 to 9 are spread from code 48 to code 57. Subtracting the code value of zero, will essentially generate the result you desire. The good thing about this approach, is that it does not depend on the default encoding you are using (although I don't know if there is an encoding which does not respect the ASCII characters' order), and because you do not need to memorize the character code yourself.

Upvotes: 1

D Stanley
D Stanley

Reputation: 152501

Because, by default, casting a character to an integer returns the ASCII value of that character.

You could use int.Parse() instead:

var stringNumber = 93.ToString(CultureInfo.InvariantCulture);
var stringArray = stringNumber.ToCharArray();
var intArray = stringArray.Select(x => int.Parse(x)).ToArray();

Upvotes: 6

csharpwinphonexaml
csharpwinphonexaml

Reputation: 3683

You get 57 instead of 9 because casting a char to int will get char's decimal value

9 is 57 according to this encoding http://www.fileformat.info/info/unicode/char/0039/index.htm

3 is 51 according to this encoding(same) http://www.fileformat.info/info/unicode/char/0033/index.htm

To get the actual value of the char you must parse that value to int int.Parse(x):

        var stringNumber = 93.ToString(CultureInfo.InvariantCulture);
        var stringArray = stringNumber.ToCharArray();
        var intArray = stringArray.Select(x => int.Parse(x)).ToArray();

Upvotes: 2

Habib
Habib

Reputation: 223197

You can use Char.GetNumericValue like:

int[] intArray = 93.ToString()
                 .ToCharArray()
                 .Select(r=> (int) char.GetNumericValue(r))
                 .ToArray();

If your number is int you can look at this answer for getting an int array without converting it to string.

Upvotes: 3

Liam McInroy
Liam McInroy

Reputation: 4356

This is because that is the ASCII code for 3 and 9.

It is converting correctly, You are just getting the char code. Try

int.Parse(x)

Upvotes: 1

SOFKiNG
SOFKiNG

Reputation: 385

var intArray = stringArray.Select(x => int.Parse(x)).ToArray();

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You are casting a char to its numeric representation. Try this using int.Parse():

var stringNumber = 93.ToString(CultureInfo.InvariantCulture);
var stringArray = stringNumber.ToCharArray();
var intArray = stringArray.Select(x => int.Parse(x)).ToArray();

Upvotes: 2

Tigran
Tigran

Reputation: 62248

Because you convert '9' char to int code rapresentation.

stringArray.Select(x => int.Parse(x)).ToArray();

Upvotes: 1

Related Questions