Archived

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

Pilatus

casting int to string

Recommended Posts

If you are using C++:

#include <sstream>   // for std::stringstream (and std::istringstream, etc.)

int someNumber = ...;

std::stringstream ss;
ss << someNumber;
std::string strNumber;
ss >> strNumber;


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

Share this post


Link to post
Share on other sites
quote:
Original post by Pilatus
itoa(int ,char*,10)
works fine


This question gets asked every week - you should use the search facility to find previous answers. For the record, you should avoid itoa(), which is not a Standard C or C++ function. sprintf and stringstream are.

Share this post


Link to post
Share on other sites
quote:
Original post by granat
Nah forget the search feature...It was a simple question and easy for us to answer.

Even more shameful then that three people managed to recommend a non-standard function. We don't need that sort of misinformation on the boards. Last time I looked, the forum name was `For Beginners' not `By Beginners'.

[edited by - SabreMan on July 3, 2003 11:49:46 AM]

Share this post


Link to post
Share on other sites
The best way using C is strtol or strtoll in stdlib.h

char *stringToConvert;
char *endPointer;
long integer;

integer = strtol(stringToConvert, &endPointer, 10);
if (endPointer == stringToConvert) { /* stringToConvert is not an integer */ }

Share this post


Link to post
Share on other sites