looking for an int to string converter

Started by
7 comments, last by PPCThug 20 years, 2 months ago
I looked in the visual c++ help file and couldn''t find one, I need a function that can turn a long unsigned int into a char string. Bloodshed Dev-C++ 4.9.8.0 DX 9.0a DX SDK 6.1 win98 #define WIN32_LEAN_AND_MEAN the Particle Projection Cannon fires a shimmering blue bolt, much like a cross between lightning and a sine wave that ripples along its path.
Bloodshed Dev-C++ 4.9.8.0 Mingw DX 9.0a DX SDK 6.1win2k#define WIN32_LEAN_AND_MEANthe Particle Projection Cannon fires a shimmering blue bolt, much like a cross between lightning and a sine wave that ripples along its path.mechwarrior 2 mercenaries, 4 particle projection cannons, thug chassis
Advertisement
look up sprintf:

int foo = 1000;char buffer[256];sprintf(buffer, "%d", foo);


you could always then load the buffer into a std::string and truncate it to make it as short as possible.

-me
works thanks!
Bloodshed Dev-C++ 4.9.8.0 Mingw DX 9.0a DX SDK 6.1win2k#define WIN32_LEAN_AND_MEANthe Particle Projection Cannon fires a shimmering blue bolt, much like a cross between lightning and a sine wave that ripples along its path.mechwarrior 2 mercenaries, 4 particle projection cannons, thug chassis
The C++ way to do this (sprintf is C) is to use ostringstream.

#include <sstream>using namespace std;int i = 10;ostringstream oss;string s;oss << i;s = oss.str();



Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on February 19, 2004 7:23:53 PM]
神はサイコロを振らない!
itoa also works? i think
quote:Original post by Mupp
itoa also works? i think
Non-standard, among other problems.
I''m suprised no one has suggested boost::lexical_cast<> yet.
quote:Original post by SiCrane
I''m suprised no one has suggested boost::lexical_cast<> yet.


Last time I checked lexical_cast (that would be 30 seconds ago),
it''s just a fancier wrapper around stringstream.


Kami no Itte ga ore ni zettai naru!
神はサイコロを振らない!
True, but using a lexical_cast can protect you from certain bugs. For example, about a week and a half ago, a forum poster was having trouble using a stringstream method because he was reusing the stringstream object and the eof bit was being set after extraction, causing subsequent uses of the stringstream object to fail.

This topic is closed to new replies.

Advertisement