Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to convert from number to text in C++? (http://www.velocityreviews.com/forums/t459672-how-to-convert-from-number-to-text-in-c.html)

Huyvtq 01-06-2007 03:30 AM

How to convert from number to text in C++?
 
What's the function I can use ?? Help me!
Thanks.


Jim Langston 01-06-2007 03:50 AM

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.



JeffCameron 01-06-2007 06:32 AM

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);


Shawn McGrath 01-06-2007 06:45 AM

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.


Jim Langston 01-06-2007 08:57 AM

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.



Thomas J. Gritzan 01-06-2007 09:06 AM

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

Jim Langston 01-06-2007 09:16 AM

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.



Rolf Magnus 01-06-2007 12:35 PM

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.


JeffCameron 01-07-2007 07:55 AM

Re: How to convert from number to text in C++?
 
Ah yes a slip of the tongue. Leave the new keywork out.

Jeff Cameron


Grizlyk 01-12-2007 11:48 AM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57