Skip to main content
GameDev.net gamedev.net
🔒 Locked

Can you parse integers?

Started by Billy Lee Aug 27, 2003 at 9:42 AM 18 replies 2.1k views
Original Post
Billy Lee
Billy Lee
Is it possible to parse each digit in an int? For example, for the integer 100, can you extract ''1'', ''0'' and ''0'' from it? I need to do it to display the score, so that instead of using normal text, I can use my own graphics for the numbers.
Jimfing
Jimfing
look up "sprintf" in MSDN - it is your friend.
Fruny
Fruny

#include <string>
#include <sstream>

template<class T>
string ToString( const T& value )
{
std::ostreamstring oss;
oss << value;
return oss.str();
}

string str = ToString( 100 );

assert( str[0] == ''1'' );
assert( str[1] == ''0'' );
assert( str[2] == ''0'' );




[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Pipo DeClown
Pipo DeClown
Go Go Go Go Go Fruny.. it''s your birthday.. we''re gonna pary like it''s your..

.lick
Fourty_Two
Fourty_Two
Why not just use:

char* score[5];
int currentScore = 100;

itoa( score, currentScore, 10 );


then you have a char array with the score.

score[0] = ''1'' etc
Lektrix
Lektrix
Pfff, Fruny. Use templated std::basic_string and std::basic_ostringstream (character traits). It makes for a more generic function, for example you can use std::wstring with it.

See boost::lexical_cast.

Does that mean that it's my birthday now?

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on August 27, 2003 12:24:07 PM]
Billy Lee
Billy Lee
That itoa one sounds promising. However, I need it in int form so I can use the value to index into my array of numbers, i.e. 100 would index into numbers[1], numbers[0] and numbers[0]. I guess I''ll have to convert from char to int again. Unless there is a more efficient way. Is there?
rypyr
rypyr

char strNum[10];
char* chPtr = strNum;
int n = ...

ltoa(n, strNum, 10);
while(chPtr) {
int nDigit = (int)(*chPtr - ''0'');
// do something with nDigit
++chPtr;
}

Special handling is needed for minus signs...
Odd the Hermit
Odd the Hermit
quote:
Original post by Billy Lee
That itoa one sounds promising. However, I need it in int form so I can use the value to index into my array of numbers, i.e. 100 would index into numbers[1], numbers[0] and numbers[0]. I guess I''ll have to convert from char to int again. Unless there is a more efficient way. Is there?


100 % 10 == 0.
(100/10)%10 == 0.
((100/10)/10)%10 == 1.

So, in general:

int score = 100;
// ...

int ones = score % 10;
score = score / 10;
int tens = score % 10;
score = score / 10;
int hundreds = score % 10;


Of course, at this point, score is meaningless, so you probably want to do all of this on a copy, but you get the point.

-Odd the Hermit
rypyr
rypyr
quote:
Original post by Odd the Hermit
100 % 10 == 0.
(100/10)%10 == 0.
((100/10)/10)%10 == 1.



Not bad. Unfortunately this will make scores like 5 come out as 005, but you could have special handling for that if need be (or maybe that''s even acceptable). Also, negative scores will require special handling.

Odd the Hermit
Odd the Hermit
quote:
Original post by rypyr
Not bad. Unfortunately this will make scores like 5 come out as 005, but you could have special handling for that if need be (or maybe that's even acceptable). Also, negative scores will require special handling.



True; perhaps a more robust version would be:

int score;
// ...

bool negative = false;
int ones=0, tens=0, hundreds=0;
if( score < 0 )
{
negative = true;
score = -score;
}
if( score > 0 )
{
ones = score % 10;
score = score / 10;
if( score > 0 )
{
tens = score % 10;
score = score / 10;
if( score > 0 )
hundreds = score % 10;
}
}


Of course, that's an "off the top of my head" implementation, so I'm sure the code could be made a bit cleaner. Still, this will work for any score from -999 to 999, without the overhead of converting to a string and back. (Note: division does have some inherent slowness, but I think it's still faster than converting to/from a string; haven't tested this.) Expanding to handle bigger ranges of scores should be obvious.

-Odd the Hermit

EDIT: realized that I'd gone slightly overboard with the copy/paste.

[edited by - Odd the Hermit on August 28, 2003 11:08:13 AM]
Billy Lee
Billy Lee
Thanks dudes, that works. My score is always positive so I don''t need to take account of negative numbers.
Puzzler183
Puzzler183
Jeez, haven''t you people ever heard of the modulus operator?! For example the first digit is x % 10. Then the second is (x % 100) - the first digit; and so on.
Hawkeye3
Hawkeye3
atoi( ) does this perfectly... no need for any hacks.
Puzzler183
Puzzler183
quote:
Original post by Hawkeye3
atoi( ) does this perfectly... no need for any hacks.


Modulus isn''t a hack, it''s simply basic arithmetic that ALL programmers SHOULD know.
Shannon Barber
Shannon Barber
Are you using the digits to index a font-character array?
If so, then itoa is the best way. It''s efficent and yields the result you want, 5 is "5" not "005".

To index your array from the result of itoa:

char buf[32];
itoa(buf, x, 10);
for(char* c=&buf[0]; *c!=0; ++c)
{
int d = *c - ''0'';
assert(d>=0 && d<=9);
number[d];
}
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Shannon Barber
Shannon Barber
It''s not exactly a hard function to write if it''s missing.

Using stringstream would result in memory (de)allocation, perhaps multipl allocations, per string displayed.
The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.