2

I am trying to take the user's input and convert it into an array of ints. The problem is that when I choose 4 numbers, e.g. 2463 the output for the string is correct, but the output for the int array is incorrect and seems to be the number chosen + 48.

I'm not really sure why this is occurring. Thanks for any help given.

string userChoiceAsString;
int[] userChoice = new int[4];

userChoiceAsString = (Console.ReadLine());

for (int i = 0; i < userChoiceAsString.Length; i++)
{
    userChoice[i] = userChoiceAsString[i];
    Console.WriteLine(userChoice[i]);
    Console.WriteLine(userChoiceAsString[i]);
}
3
  • 4
    You should look at the ASCII table. Commented Jan 1, 2015 at 13:57
  • 1
    asciitable.com Commented Jan 1, 2015 at 13:57
  • Hint: Take a look at what happens when the input is "X Y Z". Commented Jan 1, 2015 at 21:26

4 Answers 4

2

This is reasonable, because when a user inputs the character 2, this corresponds to the decimal 50. This is associated with the ASCII table.

Please have a look here.

In order to avoid this, you should try to parse the each character, like below:

userChoice[i] = Int32.Parse(userChoiceAsString[i].ToString());

or you could make use of Char's method GetNumericValue which returns a float number and then cast this to an int.

userChoice[i] = (int)Char.GetNumericValue(userChoiceAsString[i]);
Sign up to request clarification or add additional context in comments.

1 Comment

.NET strings are counted sequences of UTF-16 code-units, one or two of which encode a Unicode codepoint. So, instead of looking at an ASCII table, you should look at a Unicode table. Here's a pretty partial one. Here's the full Unicode table.
2

You need to convert the ASCII value to int using int.Parse(), otherwise you're printing the ASCII value.

Comments

0

try :

userChoice[i] = (int)Char.GetNumericValue(userChoiceAsString[i]);

Comments

0

The number 0 that we all know is represented by the character that has a numerical value of 48 in ASCII, Unicode and several other character sets. The letter A is 65, etc. This has literally nothing to do with the actual numerical value, since characters also represent letters and millions of other glyphs which have no numerical value.

You can look at the Char.GetNumericValue method, but note that it also returns the numerical value of pi, e, and other numerals from all kinds of character sets.

What we usually do is check if the character is between '0' and '9', and if so, get the value as (int)ch - (int)'0'. This returns how far away your number is from the '0' character, thus the 0..9 value you are looking for. It just so happens that the characters '0'..'9' are after each other (it just makes sense, but it's not necessarily so).

You can also try int.Parse on a single character or the whole string. That also handles the usual decimal digits as you would expect.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.