473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

Jan 6 '07 #1
12 2268
"Huyvtq" <hu****@gmail.comwrote in message
news:11**********************@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.
Jan 6 '07 #2
#include <stdio.h>
#include <string>
....

int number;
char s1[256];

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

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

Jan 6 '07 #3

Jim Langston wrote:
"Huyvtq" <hu****@gmail.comwrote in message
news:11**********************@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.

Jan 6 '07 #4

"JeffCameron" <ca********@gmail.comwrote in message
news:11**********************@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.
Jan 6 '07 #5
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
Jan 6 '07 #6
"Thomas J. Gritzan" <Ph*************@gmx.dewrote in message
news:en**********@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.
Jan 6 '07 #7
Jim Langston wrote:
"Huyvtq" <hu****@gmail.comwrote in message
news:11**********************@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.

Jan 6 '07 #8
Ah yes a slip of the tongue. Leave the new keywork out.

Jeff Cameron

Jan 7 '07 #9

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

Jan 12 '07 #10
Grizlyk wrote:
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

Jan 12 '07 #11
Greg wrote:
>snprintf can be used (if implemented) as safe version of sprintf

In a conforming C++ implementation, std::snprintf() will always be
implemented.
No, it won't.
The routine is declared in <cstdio>.
Actually, that's even forbidden for a conforming implementation.

Jan 13 '07 #12
Greg wrote:
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?

Jan 14 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a...
2
by: Phil Stanton | last post by:
When designing a new form or report, the Default ForeColor is often something like -2147483640 which is the colour of Windows text (possibly black) and the default backColor is -2147483643...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
3
by: mrajanikrishna | last post by:
Hi Friends, I am accepting a number from the user entered in a textbox. I want to assign to a variable in my code and assignt this to that variable. double num1 = (double)txtNum1.text; ...
5
by: Elainie | last post by:
I would like to convert data numbers to text.... i.e. 1 = Inactive 2 = Due 3 = Received 4 = Problems 5 = Cleared Their is numbers in the fields at the moment but they need to be converted to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.