Converting int's to char* 's
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?
char text [64];
sprintf( text, "%ld", hitPoints );
// text == "100"
~CGameProgrammer( );
sprintf( text, "%ld", hitPoints );
// text == "100"
~CGameProgrammer( );
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]
[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 *.
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( );
[edited by - CGameProgrammer on April 29, 2002 12:19:07 AM]
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
Popular Topics
Advertisement