How to convert from number to text in C++?

J

Jim Langston

Huyvtq said:
What's the function I can use ?? Help me!
Thanks.

The best choice, IMO, is to use a stringstream.

std::stringstream Convert;
Convert << 12345;
std::string NumAsString;
Convert >> NumAsString;

At this point the std::string Convert will contain "12345".

Another, although I feel poorer choice, is itoa which uses char arrays
rather than std::string.
 
J

JeffCameron

#include <stdio.h>
#include <string>
....

int number;
char s1[256];

number = 42;
sprintf(s1, "%d", number);

std::string s = new std::string(s1);
 
S

Shawn McGrath

Jim said:
The best choice, IMO, is to use a stringstream.

std::stringstream Convert;
Convert << 12345;
std::string NumAsString;
Convert >> NumAsString;
...

I agree with the use of a stringstream, but you can most likely skip
the Convert >> NumAsString part and use Convert.str(); to obtain a a
string from the stringstream.
 
J

Jim Langston

JeffCameron said:
#include <stdio.h>

#include said:
#include <string>
...

int number;
char s1[256];

number = 42;
sprintf(s1, "%d", number);

std::sprintf( s1, "%d", number );
std::string s = new std::string(s1);

And, yes, that is another way to convert to a char array.

But, std::strings are prefered.
 
T

Thomas J. Gritzan

JeffCameron said:
#include <stdio.h>
#include <string>
...

int number;
char s1[256];

number = 42;
sprintf(s1, "%d", number);

std::string s = new std::string(s1);

Will not compile.
"new std::string" returns a pointer.
 
J

Jim Langston

Thomas J. Gritzan said:
JeffCameron said:
#include <stdio.h>
#include <string>
...

int number;
char s1[256];

number = 42;
sprintf(s1, "%d", number);

std::string s = new std::string(s1);

Will not compile.
"new std::string" returns a pointer.

Oooh, I missed that one myself.
 
R

Rolf Magnus

Jim said:
The best choice, IMO, is to use a stringstream.

std::stringstream Convert;
Convert << 12345;
std::string NumAsString;
Convert >> NumAsString;

At this point the std::string Convert will contain "12345".

Another, although I feel poorer choice, is itoa which uses char arrays
rather than std::string.

itoa is also not a standard function, so it's non-portable.
 
G

Greg

Grizlyk said:
JeffCameron ÐÉÓÁÌ(Á):
int number;
char s1[256];

number = 42;
sprintf(s1, "%d", number);

snprintf can be used (if implemented) as safe version of sprintf

In a conforming C++ implementation, std::snprintf() will always be
implemented. The routine is declared in <cstdio>.

std::snprintf() is a much safer choice than std::sprintf() when
converting a number to a string. In fact sprintf() which should never
be called at all in a modern application - due to the risk of
overruning the buffer by calling the function.

Of course snprintf() is not perfectly safe either, since a buffer
overrun - while less likely - is still possible. With a string class
object, however, there is no risk at all of a buffer overrun. Therefore
using std::string for the number-to-string conversion is the best
option; and std::string should be the default choice of for managing
string values in a C++ program.

Greg
 
G

Grizlyk

Greg said:
Of course snprintf() is not perfectly safe either, since a buffer
overrun - while less likely - is still possible.

Do not understand - overrun or no? I suppose, data can be truncated by
small buffer and buffer can not be extended while snprintf executing,
but external memory can not be damaged. Yes?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top