# 48 getting added to number

I’ve been coding for a while, and I’ve never been this baffled before. My code is telling me that a number is equal to 48, but not really.

if (num > 21 && num < 30)
{
Debug.Log(numTemp);
char c = num.ToString()[1];
numTemp = c;
Debug.Log(numTemp);
single();

if (answer.text == "twenty" + "-" + temp)
{
reset();
}
}

The problem is with numTemp. The first Debug.Log says “0” every time. But then, the second one is always equal to c + 48. There is NO other reference to numTemp other than here. Above it is created as

private int numTemp;

So if the randomizer comes up with 28 we separate the 8 and single(); checks the number (8) and sets temp equal to “eight” Then I can check if answer.text == “twenty” + “-” + “eight”

The problem is, single(); doesn’t find 8 it finds 56. I have no idea why and I’m very confused.

Single(); sets temp equal to the text equivalent 1 - 9 just so you know

Hi @Prune_Dealer

You are casting characters to integers without converting it to numbers, so basicly you are assigning an ascii code of c character to numTemp variable.

So if you have “0” you are getting its ascii code 48 and if you have “8” you getting 56 as an integer value.

If you want to get number from string you can do something like this:

int.Parse(num.ToString())

or if you want convert char to int you can do this:

(int)(c-'0')

Basically to get value of the digit from ascii code you only need to subtract ascii code of “0” (48) from it.

More about ascii table: ASCII – Wikipedia, wolna encyklopedia