How could I convert a Long Long Int into string?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
BestIvan
New poster
Posts: 3
Joined: Thu Apr 24, 2003 6:18 am

How could I convert a Long Long Int into string?

Post by BestIvan »

i can convert a int by itoa()
i can convert a long by ltoa()
but how about long long?
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

Post by kmhasan »

So far as i know, itoa and ltoa are not ANSI compliant.

I use sprintf(str,"%d",num) for int, and sprintf(str,"%lld",num) for long long int. Here str is the string, and num is the integer variable.
ChristopherH
New poster
Posts: 31
Joined: Sun Feb 23, 2003 9:18 pm
Location: Waterloo, Ontario, Canada

Post by ChristopherH »

Alternatively, you can use an ostringstream for all kinds of output formatting, including long long.

[cpp]
#include <sstream>

string longlongtostring(long long x) {
ostringstream os;
os << x;
return os.str();
}
[/cpp]
kmhasan
Problemsetter
Posts: 107
Joined: Fri Oct 26, 2001 2:00 am
Location: Canada
Contact:

Post by kmhasan »

Cool. :D
Didn't know how to do it in C++. Thanks.
anupam
A great helper
Posts: 405
Joined: Wed Aug 28, 2002 6:45 pm
Contact:

Post by anupam »

well i think sprintf is the best way to do all the converts to string..
right?
"Everything should be made simple, but not always simpler"
Post Reply

Return to “C++”