By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,301 Members | 1,464 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,301 IT Pros & Developers. It's quick & easy.

converting int to string

P: n/a
Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help

Michael
Jun 14 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Michael wrote:
Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help

Michael


fuction "sprintf"

Jun 14 '06 #2

P: n/a
Michael schrieb:
I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?


ret = snprintf(buffer, n, "%d", 1234);
if (ret < 0 || ret >= n) {
/* something went wrong || the buffer size (n) was not
** large enough */
}
or, if your library does not have the C99 function snprintf(),
use its unsafe cousin, sprintf(), with a "large enough" buffer.

For arbitrary base, have a look at itoa() which is not a part of
the standard library. Recently, I posted an extended safer version
of itoa() to show how one could write his or her own, see
<4e*************@individual.net>.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jun 14 '06 #3

P: n/a
On Wed, 14 Jun 2006 14:12:43 +0800, "Michael" <mi*********@yahoo.com>
wrote in comp.lang.c:
Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......
How did you miss:

"13.1 How can I convert numbers to strings (the opposite of atoi)? Is
there an itoa function?"
If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help


The question, and its answer, are at http://c-faq.com/lib/itoa.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jun 14 '06 #4

P: n/a
Hi,

As already indicated, you can use sprintf to convert a string eg.

int number=1234;
char buffer[5];

sprintf (buffer, "%d", number);

sprintf also null terminates the string!

However, I also think there is a function called itoa() which can also
be used. However, I prefer to use sprintf.

Regards,

Richard

Jack Klein wrote:
On Wed, 14 Jun 2006 14:12:43 +0800, "Michael" <mi*********@yahoo.com>
wrote in comp.lang.c:
Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......


How did you miss:

"13.1 How can I convert numbers to strings (the opposite of atoi)? Is
there an itoa function?"
If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help


The question, and its answer, are at http://c-faq.com/lib/itoa.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


Jun 14 '06 #5

P: n/a
ri*******@latter.demon.co.uk wrote:

[ Don't top-post, please. And do snip. Corrected. ]
Jack Klein wrote:
On Wed, 14 Jun 2006 14:12:43 +0800, "Michael" <mi*********@yahoo.com>
wrote in comp.lang.c:
I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......
How did you miss:

"13.1 How can I convert numbers to strings (the opposite of atoi)? Is
there an itoa function?" The question, and its answer, are at http://c-faq.com/lib/itoa.html


As already indicated, you can use sprintf to convert a string eg.

sprintf also null terminates the string!
It had better, or it isn't a string.
However, I also think there is a function called itoa() which can also
be used.


And if you'd read that FAQ Jack linked to, you'd have known that there
isn't, in ISO C.

Richard
Jun 14 '06 #6

P: n/a
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
ri*******@latter.demon.co.uk wrote:

[...]
sprintf also null terminates the string!


It had better, or it isn't a string.


True -- but note that strncpy(), despite the name, doesn't necessarily
null-terminate its target.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 14 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.