convert int to char

J

John Carroll

Does anyone have a function or procedure for converting integers to
character strings?

Thank you,
John
 
M

Martin Ambuhl

John said:
Does anyone have a function or procedure for converting integers to
character strings?

Didn't you think this would probably be a common question? If not, then
you think you've stumbled onto unexplored territory, even though C has
been in use for a quarter century. That seems unlikely, especially for
such a common thing to do, doesn't it? So a rational person would check
the FAQ, wouldn't he? <http://www.eskimo.com/~scs/C-faq/q13.1.html>
 
A

Artie Gold

John said:
Does anyone have a function or procedure for converting integers to
character strings?
sprintf() (or better, if you have a [somewhat] C99 compatible
compiler/library, snprintf().

But please, *get a book!!!!!*

HTH,
--ag
 
W

Walter Roberson


printf() is indeed capable of converting integers to character
strings -- it just has its own ideas about where the character
strings should end up. ;-)

If one cared to go through the bother, one could, completely
within standard C for hosted environments, open a file read/write,
printf() to produce the character string output, rewind to the
beginning of the file, and read the string in to the desired
destination buffer. Sure it'd be clunky, but conformant.


Depending on how many extensions one was willing to live with, there is
even a case in which the operation might be relatively efficient -- if
one closed stdout, nmap()'d some memory, took the fd from that [which
would be the fd normally used for stdout because of the POSIX rules
about using the first available fd], used fdopen() to pull that up to a
FILE*, and used printf() to do the desired conversion, then the result
could be read back directly from the memory area nmap'd to.
Who needs simple standard sprintf() when one can use something
complicated, not generally portable, but 3001 ? ;-)
 
A

Artie Gold

Walter said:
Artie Gold said:


printf() is indeed capable of converting integers to character
strings -- it just has its own ideas about where the character
strings should end up. ;-)

If one cared to go through the bother, one could, completely
within standard C for hosted environments, open a file read/write,
printf() to produce the character string output, rewind to the
beginning of the file, and read the string in to the desired
destination buffer. Sure it'd be clunky, but conformant.


Depending on how many extensions one was willing to live with, there is
even a case in which the operation might be relatively efficient -- if
one closed stdout, nmap()'d some memory, took the fd from that [which
would be the fd normally used for stdout because of the POSIX rules
about using the first available fd], used fdopen() to pull that up to a
FILE*, and used printf() to do the desired conversion, then the result
could be read back directly from the memory area nmap'd to.
Who needs simple standard sprintf() when one can use something
complicated, not generally portable, but 3001 ? ;-)

Hmmm. Seems you're about a week late on this response (at least in my
time zone) ;-) ;-)

Cheers,
--ag

[Well, at least it's not "TCP/IP by carrier pigeon"!]
 
W

Walter Roberson

Walter Roberson wrote: [...]


Hmmm. Seems you're about a week late on this response (at least in my
time zone) ;-) ;-)
[Well, at least it's not "TCP/IP by carrier pigeon"!]

?? According to the message headers, the message that started this
thread was posted at Fri, 08 Apr 2005 12:07:41 EDT, your response was
posted at Fri Apr 08 22:25:33 CDT 2005, and my response was posted at 9
Apr 2005 04:26:55 GMT. I am in CDT myself, as you are, (even though the
posting headers are GMT); I'm pretty much due north of you. CDT is GMT-5,
so my posting was 23:26:55 CDT, or only a hair more than 1 hour after
your posting.
 
K

Keith Thompson

Walter Roberson wrote: [...]


Hmmm. Seems you're about a week late on this response (at least in my
time zone) ;-) ;-)
[Well, at least it's not "TCP/IP by carrier pigeon"!]

?? According to the message headers, the message that started this
thread was posted at Fri, 08 Apr 2005 12:07:41 EDT,
[snip]

Which is one week after April Fool's Day.
 
C

Chris Croughton

printf() is indeed capable of converting integers to character
strings -- it just has its own ideas about where the character
strings should end up. ;-)

If one cared to go through the bother, one could, completely
within standard C for hosted environments, open a file read/write,

Using freopen().
printf() to produce the character string output, rewind to the
beginning of the file, and read the string in to the desired
destination buffer. Sure it'd be clunky, but conformant.

You forgot to delete the file afterwards. Unfortunately tmpfile() can't
be used to reopen stdout (and in my experience the autodelete
functionality of tmpfile() is one of the things broken in a number of
implementations).
Depending on how many extensions one was willing to live with, there is
even a case in which the operation might be relatively efficient -- if
one closed stdout, nmap()'d some memory, took the fd from that [which
would be the fd normally used for stdout because of the POSIX rules
about using the first available fd], used fdopen() to pull that up to a
FILE*,

Which is not guaranteed to be stdout, as I read POSIX (the fd will be
the one used for stdout, but the fp won't).
and used printf() to do the desired conversion, then the result
could be read back directly from the memory area nmap'd to.
Who needs simple standard sprintf() when one can use something
complicated, not generally portable, but 3001 ? ;-)

3001?

Chris C
 
S

Stan Milam

John said:
Does anyone have a function or procedure for converting integers to
character strings?

Thank you,
John
#include <stdio.h>

int main( void ) {

int i = 1234;
char string[100];

sprintf( string, "%d", i );
puts( string );
return 0;
}
 
C

Chris Torek

If one cared to go through the bother, one could, completely
within standard C for hosted environments, open a file read/write,
printf() to produce the character string output, rewind to the
beginning of the file, and read the string in to the desired
destination buffer. Sure it'd be clunky, but conformant.

I have actually used this trick to implement snprintf().

Remember to rewind the file twice! :) (Once after the vfprintf,
once more after reading the printed output.)
 
C

CBFalconer

Neil said:
Artie said:
Neil said:
John Carroll wrote:

Does anyone have a function or procedure for converting integers
to character strings?

[top posting orrected]

No such thing.
printf()
Nope.

sprintf()

Yup!

itoa() exists, I assume it is non-standard??

It only exists if you publish its source code. For example:

char *itoa(int i)
{
if ('i' == i) return "a";
else return "";
}

which should quite reliably convert 'i's into 'a's. Maybe you now
appreciate the point?
 
K

Keith Thompson

Neil Kurzman said:
Artie Gold wrote: [...]
No such thing.

itoa() exists, I assume it is non-standard??

I listed printf() because what do you do with a string. fprintf() would
be the other destination.

You can do anything you like with a string. Printing it to stdout or
to a specified file doesn't begin to exhaust the possibilities.

printf() may convert the integer to a character string internally, but
that doesn't help if you want to get at the character string (which is
what the OP was asking for).
 
C

Chris Croughton

Neil Kurzman wrote on 11/04/05 :

If it not standard, it doesn't exist :)

That's one of the stupid attitudes which encourages people to label
comp.lang.c as a bunch of ego-wankers.

Of course itoa() exists on many systems and in many program sources. It
simply isn't On Topic for this newsgroup, and can't be used portably.

Chris C
 
C

Christopher Benson-Manica

Chris Croughton said:
Of course itoa() exists on many systems and in many program sources. It
simply isn't On Topic for this newsgroup, and can't be used portably.

But can it be written portably? That's a valid question here.
 
M

Mark McIntyre

That's one of the stupid attitudes which encourages people to label
comp.lang.c as a bunch of ego-wankers.

Did you notice the smiley? Being humor-impaired is one of the other
things that causes the above reaction.
Of course itoa() exists on many systems and in many program sources. It
simply isn't On Topic for this newsgroup, and can't be used portably.

quite.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,901
Members
47,442
Latest member
KevinLocki

Latest Threads

Top