Converting int's to char* 's

Started by
46 comments, last by Megatron 21 years, 11 months ago
I have a font rendering function that takes a char* as its first argument. The trouble is, I want to print out a number that has to have operations performed on it. So if I have something like int hitPoints = 100; and I want to change hitPoints into char* text so I can call RenderFont(text,.....); What should I do?
Advertisement
char text [64];
sprintf( text, "%ld", hitPoints );
// text == "100"

~CGameProgrammer( );

~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
There''s also itoa.
---visit #directxdev on afternet <- not just for directx, despite the name
I'm assuming that you (Megatron) aren't aware that char* parameters are used to pass the address of the first character of a string—a C-style string (array of characters). So, you want to convert an int to a [C-style] string. That's what you'll ask for. It's not just pedanticism because the answer to your question, exactly how it was asked, is: (char*)myint .


[edited by - merlin9x9 on April 29, 2002 9:40:55 PM]
quote:Original post by merlin9x9
It''s not just pedanticism because the answer to your question, exactly how it was asked, is: (char*)myint .

Nope.

"I want to change hitPoints into char* text"

Changing hitPoints into text is not the same as casting it to char *.
---visit #directxdev on afternet <- not just for directx, despite the name
That''s how you phrased it in your message subject line, and that''s what I meant. But from what I actually said, you''re absolutely right.
using namespace std;...stringstream s;string str;s << hitPoints;str = s.str();s.str("");  // believe it or not, this clears the stringstreamcout << str; 


[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
I hate std::string. It sucks. What were they injecting into themselves when they came up with the idea of using the << and >> operators? Oluseyi's std version is so confusing and nonintuitive. That's what I don't use the STL.

~CGameProgrammer( );

[edited by - CGameProgrammer on April 29, 2002 12:19:07 AM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
quote:Original post by CGameProgrammer Oluseyi's std version is so confusing and nonintuitive. That's what I don't use the STL.


    string tostring(int n){ostringstream out;out<<n;return out.str();}    

Hide that away somewhere and all you'll ever have to see is tostring()...happy?

Oh, and std::string doesn't use << and >>. That would be STREAMS you're talking about.

[edited by - sjelkjd on April 29, 2002 12:39:43 AM]
quote:Original post by CGameProgrammer
That''s what (sic) I don''t use the STL.

*shrugs* Your loss.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement