Converting From Char To Int


motorollin

Member
Joined
Jul 31, 2007
Messages
163
I have a sprite called "font" which contains 10 items: the numbers 0-9. If I want to draw a number on the screen I use fontClips[n], where n is the number I want from 0-9. Before anybody asks why I don't just render the text using a TTF font, the font I am using is commercial so I can't include it with my game. I have therefore converted only the characters I need to an image to use as a sprite.

My game's score obviously needs to be stored as an integer so I can add to it. When I want to draw the score on the screen, I have to break the integer up in to individual numbers. Then I need to convert that character back to an integer to use it in the apply_surface command to place the correct digit.

For some reason the code below doesn't work. I know the integer is successfully converted to scorestr. And I know the apply_surface line works as I tried changing it to fontClips[0] instead of fontClips and it drew zeroes in place of each number of the score. So the problem seems to be with either getting the character from scorestr, or converting the character back to an integer. Can anyone help?

CODE
//Set a test score
int score=123;

//Convert the score to a string "scorestr"
stringstream ss;
string scorestr;
ss << score;
ss >> scorestr;

//Find out where to start placing the numbers
int nums = scorestr.length();
int scorewidth = nums*12;
int scorestartpos = (SCREEN_WIDTH/2)-(scorewidth/2);

//For each character in the score...
for ( int i = 0; i < nums; i++ )
{
//Convert this char back to an integer
int s=scorestr+0;

//Show the correct sprite
apply_surface( scorestartpos+(i*12), 10, font, screen, &fontClips[ s ] );
}
 
int s=atoi( scorestr ) give me "invalid conversion from `char' to `const char*' initializing argument 1 of `int atoi(const char*)' " :huh:
 
scorestr.c_str()? I don't see why you're using a string as an array of values though. If you want to convert the whole string at once, atoi will do that, and if you are storing separate scores in separate characters, you should be using an array of strings, not the string as an array. Alternatively, save yourself a lot of bother and use a standard char[] array.
 
Orkie said:
scorestr.c_str()? I don't see why you're using a string as an array of values though. If you want to convert the whole string at once, atoi will do that, and if you are storing separate scores in separate characters, you should be using an array of strings, not the string as an array. Alternatively, save yourself a lot of bother and use a standard char[] array.
I'm not storing separate scores. It is just the one score. But the score is stored as an int so it can be added to. What I'm basically trying to do is take each digit of the integer and then draw that number. So if the score is "123", it would draw a 1, then a 2, then a 3. The way I thought of doing that was converting it to a string first, then taking each character of the string and converting it back to an int to pass it to the apply_surface command. The problem seems to be that a char can't be converted to an int.

Edit - corrections
 
Last edited by a moderator:
CODE
int StringUtility::charToInt(char n)
{
if(n >= 48 && n <= 57)
n-=48;

return n;
}

char StringUtility::intToChar(int n)
{
if(n >= 0 && n <= 57-48)
n+=48;
return n;
}


From one of my classes... It's probably not the best way of doing things, but it works...
 
Here is the solution, where "scorestr" is a string containing the player's score:

CODE
for ( int i=0; i<scorestr.length(); i++ )
{
int num=scorestr-'0';
apply_surface( scorestartpos+(i*12), 10, font, screen, &fontClips[ num ] );
}


I had already tried "int num=scorestr-'0';" but I missed the quotes around the 0 which is subtracted from the character. I thought you were subtracting zero to make it think it was an integer (because it was being used like one), when further reading reveals that you are subtracting the ASCII code '0' to get the value of the digit.

PokeParadox - I think your solution is on the same principle of subtracting an ASCII code to get the value of the char.
 
I still don't see why you didn't use atoi on the whole thing, it would've been so much easier.
 
Orkie said:
I still don't see why you didn't use atoi on the whole thing, it would've been so much easier.
Because I wanted each digit as a single entity, not the whole thing as one integer. If I did that then I would have been back where I started, with an integer containing the score.
 
Last edited by a moderator:
I believe I see what you mean and Orkie seems to be missing that you want to split up the integer into its constituent characters. But I believe he is on about using atoi to convert the constituents ascii characters back into integers?
 
sam fisher said:
I believe I see what you mean and Orkie seems to be missing that you want to split up the integer into its constituent characters. But I believe he is on about using atoi to convert the constituents ascii characters back into integers?
But atoi() expects a const char*, whereas a C string is an array of chars. That's why I got errors when I tried to pass one element of a string (a char) to atoi().
 
Last edited by a moderator:
PokeParadox said:
you can use .c_str() to get a const char* from a string...
What do I need to #include to use .c_str?
 
Last edited by a moderator:
motorollin is using C strings not C++ strings.
CODE
char blah[] = "342523";
int value = atoi( blah );

However as motorollin said, pulling single digit numbers from the string probably wouldn't work with atoi since it *may* only end when it hits a null terminator.
 
ummm...

CODE

last (least significant) integer = (integerScore % 10)
next (integerScore % 100)/10
then (integerScore % 1000)/100
then (integerScore % 10000)/1000

etc



?

am i missing something?
 
Orkie said:
yaustar said:
motorollin is using C strings not C++ strings.
Doesn't look that way to me.

Oops, my bad. I was looking at the last comment the OP made rather then the code in the first post. Ignore what I said.
 
Last edited by a moderator:
Back
Top