2

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?

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
CallumVass
  • 11,288
  • 26
  • 84
  • 154

8 Answers8

6

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();
D Stanley
  • 149,601
  • 11
  • 178
  • 240
3

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.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • 3
    +1. I've been working with .NET for many years and I never encountered or used the `char.GetNumericValue` method. It's always nice to learn something new. – Ivaylo Slavov Apr 04 '14 at 13:30
2

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();
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
2

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();
csharpwinphonexaml
  • 3,659
  • 10
  • 32
  • 63
1

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

stringArray.Select(x => int.Parse(x)).ToArray();
Tigran
  • 61,654
  • 8
  • 86
  • 123
1
var intArray = stringArray.Select(x => int.Parse(x)).ToArray();
SOFKiNG
  • 385
  • 1
  • 3
  • 20
1

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)
Liam McInroy
  • 4,339
  • 5
  • 32
  • 53
1

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.

Ivaylo Slavov
  • 8,839
  • 12
  • 65
  • 108