Turn an __int64 into a string...

Started by
4 comments, last by Dookie 17 years, 9 months ago
Hi guys! Got an easy one for you. Here's what I'm trying to do... StringCbPrintf(ScoreStr, sizeof(ScoreStr), "%i", GStuff->P1_GameScore); Pretty easy. Put the value of "GStuff->P1_GameScore" into the char array "ScoreStr[]". The problem I'm having is this: P1_GameScore is a '__int64', because it can get to be a VERY big number. So when I call the above, not all of the number is put into the character array 'ScoreStr'. How do I get a '__int64' variable into a string (char array)? Thanks in advance for the help! [smile]
"The crows seemed to be calling his name, thought Caw"
Advertisement
I think you need to specify the size prefix in your format string: "%I64i" instead of just "%i". Also, make sure your ScoreStr array is large enough; 20+ characters should be sufficient.
You could also get it into a regular std::string (assuming you're using C++) just by doing:

	__int64 big = 0x1234567890ABCDE;	std::strstream strm;	strm << big << std::ends;	std::string str = strm.str();
"%llu" = long long unsigned int
"%lli" = long long int

this is what i know from gcc, it might be the same in VS03 and newer
Quote:Original post by mmelson
You could also get it into a regular std::string (assuming you're using C++) just by doing:

*** Source Snippet Removed ***


Um, how many years ago did you learn C++? It's spelled "stringstream" now, and there's no 'ends' nonsense any more.

OP:

1) Referring to char arrays as "strings" makes the baby Zahlman cry.
2) You do know that 32-bit signed integers range up to 2 billion and change, yes? Just what kind of game are you making o_O
Thanks for the info, guys!

Yeah, __int64 is probably overkill since I only want the score to cap at 999,999,999. But when I used the "%i", I couldn't move that info into a char string (because the %i truncated the score value).

So what AP was mentioning:

"%llu" = long long unsigned int
"%lli" = long long int

that should work? I'm at school so I can't test that at the moment. So I should change:

__int64 GStuff->P1_GameScore;
...
StringCbPrintf(ScoreStr, sizeof(ScoreStr), "%i", GStuff->P1_GameScore);


to this:

long unsigned int GStuff->P1_GameScore;
...
StringCbPrintf(ScoreStr, sizeof(ScoreStr), "%llu", GStuff->P1_GameScore);


Does that look correct? Please let me know and I'll try it out when I get home later today. Thanks again for the help!
"The crows seemed to be calling his name, thought Caw"

This topic is closed to new replies.

Advertisement