473,386 Members | 1,699 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,386 software developers and data experts.

converting int to string

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
6 4442

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Cyde Weys | last post by:
I'm currently working on converting a simulator program from Visual Basic 6.0 to Visual C++ .NET. I've figured out most of the stuff, but there's still one thing I haven't gotten to and I've never...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Terry | last post by:
I am converting (attempting) some vb6 code that makes vast use of interfaces. One of the major uses is to be able to split out Read-only access to an obect. Let me give you a simple (contrived)...
5
by: SMichal | last post by:
Hi, how can I parse string "? 20.000" to double ?
3
by: nvx | last post by:
Hi, I'm looking for a simple way to convert a Double to a String exactly the .ToString() does, except these two differences: - the number of decimals is given (rounding is applied if necessary),...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.