![]() |
How to convert from number to text in C++?
What's the function I can use ?? Help me!
Thanks. |
Re: How to convert from number to text in C++?
"Huyvtq" <huyvtq@gmail.com> wrote in message
news:1168054244.934936.322860@11g2000cwr.googlegro ups.com... > 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. |
Re: How to convert from number to text in C++?
#include <stdio.h>
#include <string> .... int number; char s1[256]; number = 42; sprintf(s1, "%d", number); std::string s = new std::string(s1); |
Re: How to convert from number to text in C++?
Jim Langston wrote: > "Huyvtq" <huyvtq@gmail.com> wrote in message > news:1168054244.934936.322860@11g2000cwr.googlegro ups.com... > > 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; > ... 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. |
Re: How to convert from number to text in C++?
"JeffCameron" <cameron.jp@gmail.com> wrote in message news:1168065177.039025.230890@38g2000cwa.googlegro ups.com... > #include <stdio.h> #include <cstdio> > #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. |
Re: How to convert from number to text in C++?
JeffCameron schrieb:
> #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. -- Thomas http://www.netmeister.org/news/learn2quote.html |
Re: How to convert from number to text in C++?
"Thomas J. Gritzan" <Phygon_ANTISPAM@gmx.de> wrote in message
news:ennorf$ghg$1@newsreader3.netcologne.de... > JeffCameron schrieb: >> #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. |
Re: How to convert from number to text in C++?
Jim Langston wrote:
> "Huyvtq" <huyvtq@gmail.com> wrote in message > news:1168054244.934936.322860@11g2000cwr.googlegro ups.com... >> 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. itoa is also not a standard function, so it's non-portable. |
Re: How to convert from number to text in C++?
Ah yes a slip of the tongue. Leave the new keywork out.
Jeff Cameron |
Re: How to convert from number to text in C++?
JeffCameron ΠΙΣΑΜ(Α): > int number; > char s1[256]; > > number = 42; > sprintf(s1, "%d", number); snprintf can be used (if implemented) as safe version of sprintf http://www.die.net/doc/linux/man/man3/snprintf.3.html |
| All times are GMT. The time now is 06:00 PM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.