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

Converting numbers to strings

Started by Nodger Aug 16, 2003 at 7:17 AM 6 replies 1k views
Original Post
Nodger
Nodger
Does anyone know of a library compatible with Visual C++ that will quickly and easily convert all types of numbers (ints,floats,UINTs,BOOl''s,everything), to strings. and when i say string I mean a char* not the string class provided with C++ (im working with that poxy Windows OS, so i should probably say a PZSTR or whatever). As Kent Brockman says: "ONLY TIME WILL TELL."
Nodger
Nodger
sprintf? whats that?

i want to convert datatypes here not print to a dos console

As Kent Brockman says:
"ONLY TIME WILL TELL."
Nodger
Nodger
sorry. my mistake. that will do fine.
Tyson
Tyson
My favorite is stringstream. IE)


#include<stringstream>

std::stringstream s;
float x = 0.f;
int y = 1;
double z = 0.0;

s << "Numbers are:" << x << y << z;

and when you need a string you can call s.str() to get a std::string back.


Not a usefull or pretty example but you get the point. sprintf requires you to make a guess (or know) the size of the buffer you are writing into before hand, wich can cause buffer overruns if your output ends up being longer than the buffer you allocated. You can use vsnsprintf if you really want sprintf. This function takes the size of the buffer and will return -1 in the event of an overflow situation.

--Tyson


============================
A guy, some jolt, and a whole lot of code

[edited by - tyson on August 16, 2003 10:07:01 AM]
============================A guy, some jolt, and a whole lot of code :)
Lektrix
Lektrix
Yes, but he said "char* not the string class provided with C++", so he'd have to use std::string::c_str() to get the const char*. Incidentally, you include sstream, not stringstream (there is no such file), for std::stringstream.

But yes, C++ string streams are sexy.

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

[edited by - Lektrix on August 16, 2003 1:32:50 PM]
Raloth
Raloth
*cough* add to FAQ *cough*
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...

Topic Locked

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

Sign in to reply to this topic.