Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nodger

Converting numbers to strings

This topic is 5599 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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."

Share this post


Link to post
Share on other sites
Advertisement
sprintf? whats that?

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

As Kent Brockman says:
"ONLY TIME WILL TELL."

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!