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

convert int to char

P: n/a
Does anyone have a function or procedure for converting integers to
character strings?

Thank you,
John
Nov 14 '05 #1
Share this Question
Share on Google+
30 Replies


P: n/a
John Carroll wrote:
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>
Nov 14 '05 #2

P: n/a
John Carroll wrote:
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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #3

P: n/a
itoa()
printf()
sprintf()

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

Thank you,
John


Nov 14 '05 #4

P: n/a
Neil Kurzman wrote:
John Carroll wrote:

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

Thank you,
John

[top posting orrected]
itoa()
No such thing.
printf()
Nope.
sprintf()
Yup!

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #5

P: n/a
In article <3b*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
Neil Kurzman wrote:
John Carroll wrote:
Does anyone have a function or procedure for converting integers to
character strings?
printf()
Nope.


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 ? ;-)
--
Oh, to be a Blobel!
Nov 14 '05 #6

P: n/a
Walter Roberson wrote:
In article <3b*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
Neil Kurzman wrote:
John Carroll wrote:

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


Nope.

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"!]
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #7

P: n/a
In article <3b*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
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.
--
"I want to make sure [a user] can't get through ... an online
experience without hitting a Microsoft ad"
-- Steve Ballmer [Microsoft Chief Executive]
Nov 14 '05 #8

P: n/a
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) writes:
In article <3b*************@individual.net>,
Artie Gold <ar*******@austin.rr.com> wrote:
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.

--
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.
Nov 14 '05 #9

P: n/a
On 9 Apr 2005 04:26:55 GMT, Walter Roberson
<ro******@ibd.nrc-cnrc.gc.ca> wrote:
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
Nov 14 '05 #10

P: n/a
John Carroll wrote:
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;
}
Nov 14 '05 #11

P: n/a
In article <d3**********@canopus.cc.umanitoba.ca>
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
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.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #12

P: n/a


Artie Gold wrote:
Neil Kurzman wrote:
John Carroll wrote:

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

Thank you,
John


[top posting orrected]
> itoa()


No such thing.
> printf()


Nope.
> sprintf()


Yup!

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays


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.
Nov 14 '05 #13

P: n/a
Neil Kurzman wrote:
Artie Gold wrote:
Neil Kurzman wrote:
John Carroll wrote:

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


[top posting orrected]
itoa()


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?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #14

P: n/a
Neil Kurzman <ns*@mail.asb.com> writes:
Artie Gold wrote:

[...]
> itoa()


No such thing.
> printf()


Nope.
> sprintf()


Yup!
>


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

--
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.
Nov 14 '05 #15

P: n/a
Neil Kurzman wrote:
itoa() exists, I assume it is non-standard??


itoa is the homework exercises from K&R, section 3.6, Loops - Do-while

--
pete
Nov 14 '05 #16

P: n/a
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??


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

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

Nov 14 '05 #17

P: n/a
On Mon, 11 Apr 2005 19:17:45 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??


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
Nov 14 '05 #18

P: n/a
Chris Croughton <ch***@keristor.net> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #19

P: n/a
On Mon, 11 Apr 2005 19:40:42 +0100, in comp.lang.c , Chris Croughton
<ch***@keristor.net> wrote:
On Mon, 11 Apr 2005 19:17:45 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??
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.


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.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Nov 14 '05 #20

P: n/a
Chris Croughton <ch***@keristor.net> writes:
On Mon, 11 Apr 2005 19:17:45 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??
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.


I agree that the attitude can go a bit too far sometimes. For
example, it's absurd to assert that the open() and close() functions
don't exist. They do exist as part of the POSIX standard (and a
non-POSIX program is free to define functions, or other entities, with
the same names). But the POSIX-specific functions are off-topic here
in comp.lang.c.
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.


Does it really? I just checked several systems I have access to; none
of them define an itoa() function.

--
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.
Nov 14 '05 #21

P: n/a
Christopher Benson-Manica wrote:
Chris Croughton <ch***@keristor.net> spoke thus:
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.


I'll answer that as soon as you define exactly what it does. I put
up a valid implementation yesterday or so, didn't you like it? I
believe it reliably converted an 'i' to an 'a', and was completely
portable.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #22

P: n/a
CBFalconer <cb********@yahoo.com> spoke thus:
I'll answer that as soon as you define exactly what it does. I put
up a valid implementation yesterday or so, didn't you like it? I
believe it reliably converted an 'i' to an 'a', and was completely
portable.


Sorry for implying otherwise...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #23

P: n/a
On Mon, 11 Apr 2005 21:28:08 GMT, Keith Thompson
<ks***@mib.org> wrote:
I agree that the attitude can go a bit too far sometimes. For
example, it's absurd to assert that the open() and close() functions
don't exist. They do exist as part of the POSIX standard (and a
non-POSIX program is free to define functions, or other entities, with
the same names). But the POSIX-specific functions are off-topic here
in comp.lang.c.


Yes, exactly. It's one thing to say "Those functions are off-topic
here, because they aren't part of Standard C, try somewhere like
comp.unix.programmer", and a different one to say a blanket "it doesn't
exist". The latter will just get the OP's response "Yes it does!".
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.


Does it really? I just checked several systems I have access to; none
of them define an itoa() function.


Borland defines it for Win32, and says under 'portability' that it's a
Win32 function. I remember it being on DOS as well. I've also seen it
re-implemented in code for *ix software, so it 'exists' on those systems
even if not in a system library.

Chris C
Nov 14 '05 #24

P: n/a
Chris Croughton wrote on 11/04/05 :
On Mon, 11 Apr 2005 19:17:45 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??


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.


A serious lack of humour... Get a life...

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

..sig under repair

Nov 14 '05 #25

P: n/a
In article <mn***********************@YOURBRAnoos.fr>,
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote:
Chris Croughton wrote on 11/04/05 :
On Mon, 11 Apr 2005 19:17:45 +0200, Emmanuel Delahaye
<em***@YOURBRAnoos.fr> wrote:
Neil Kurzman wrote on 11/04/05 :
itoa() exists, I assume it is non-standard??

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.


A serious lack of humour... Get a life...


But that *is* clc.

A sense of humor is OT here. And, as with most OT posts, the regulars will
generally be more than willing to supply names of other newsgroups where
what you seek is on topic. For example, I suggest: alt.folklore.urban.

Nov 15 '05 #26

P: n/a
> From: "John Carroll" <or*******@yahoo.com>
Does anyone have a function or procedure for converting integers to
character strings?


Your question is hopelessly vague. There are all sorts of mappings from
integers to character strings. Here are just a few (with quote marks
omitted):

Arabic numerals:
Decimal: 42
Octal: 52
Hexadecimal: 2A
Binary: 101010
Unary with stop bit: 1111111111111111111111111111111111111111110
Tally:
Original: IIIII IIIII IIIII IIIII IIIII IIIII IIIII IIIII II
Old: <http://www.rawbw.com/~rem/AsciiArt/TallyOldAA.txt>
New: <http://www.rawbw.com/~rem/AsciiArt/TallyNewAA.txt>
Roman numerals:
Old: XXXXII
New: XLII
Cardinal:
English: forty-two
German: zwei und vierzig (Note: AltaVista Babelfish gets this wrong!)
French: quarante deux
Spanish: cuarenta dos
Portuguese: quarenta dois
Ordinal:
English: forty-second
French: quarante seconde
Spanish: cuarenta segundos
Portuguese: quarenta segundos
German: (Babelfish says vierzig zweites, but I don't believe it's correct.)
Traditional base 12:
English: three dozen and six
French: trois douzaines et six
German: drei Dutzend und sechs
Spanish: tres docenas y seises
Traditional by Abraham Lincoln:
English: two score and two

So do you want a single function capable of all those conversions, or what?

Oh, you just want a single character? That's not possible because the
range of integers is larger than the range allowed for a single
character, whether a single byte (US-ASCII or Latin-1) or double byte
(subset of UniCode). If you restrict the range of integers to what
will fit in a single byte, then you have your choice of Latin-1 or
several other system-dependent codes. If you restrict the range of
integers to what will fit in 7 bits, you have your choice of US-ASCII
or EBCDIC. (C provides support only for US-ASCII, via a "cast".) If you
restrict the range of integers to 0 thru 35, you have extended
IBM-hexadecimal: 0123...9ABCDE...XYZ. If you restrict the range to only
0 thru 15, C provides support via sprintf(&ch, "%x", intval).
Nov 15 '05 #27

P: n/a
In article <RE***************@Yahoo.Com>,
Robert Maas, see http://tinyurl.com/uh3t <re*****@Yahoo.Com> wrote:
From: "John Carroll" <or*******@yahoo.com>
Does anyone have a function or procedure for converting integers to
character strings?


Your question is hopelessly vague. There are all sorts of mappings from
integers to character strings. Here are just a few ...


and the void would be calling
--
7842++
Nov 15 '05 #28

P: n/a
re*****@Yahoo.Com (Robert Maas, see http://tinyurl.com/uh3t) wrote:
# > From: "John Carroll" <or*******@yahoo.com>
# > Does anyone have a function or procedure for converting integers to
# > character strings?

char b[sizeof(int)*CHAR_BIT/3+1];
sprintf(b,"%d",int_value);

The formatted integer is in b.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
GERBILS
GERBILS
GERBILS
Nov 15 '05 #29

P: n/a
In article <RE***************@Yahoo.Com>,
Robert Maas, see http://tinyurl.com/uh3t <re*****@Yahoo.Com> wrote:
Your question is hopelessly vague. There are all sorts of mappings from
integers to character strings. Here are just a few Traditional base 12:
English: three dozen and six


It seems to me that more traditional would be
"three and a half dozen"

--
History is a pile of debris -- Laurie Anderson
Nov 15 '05 #30

P: n/a
On Tue, 28 Jun 2005 17:12:27 -0700, Robert Maas, see http://tinyurl.com/uh3t
<re*****@Yahoo.Com> wrote:
From: "John Carroll" <or*******@yahoo.com>
Does anyone have a function or procedure for converting integers to
character strings?
Your question is hopelessly vague. There are all sorts of mappings from
integers to character strings. Here are just a few (with quote marks
omitted):

Arabic numerals:
Tally:
Roman numerals:
Cardinal:
English: forty-two
German: zwei und vierzig (Note: AltaVista Babelfish gets this wrong!)


So do you <g>. It's zweiundvierzig, all one word <g>.
Ordinal:
Traditional base 12:
English: three dozen and six
Three and a half dozen would be more likely.
Traditional by Abraham Lincoln:
Base64 (as used in HTLM/XML/etc.):

Aq==
Oh, you just want a single character? That's not possible because the
range of integers is larger than the range allowed for a single
character, whether a single byte (US-ASCII or Latin-1) or double byte
(subset of UniCode). If you restrict the range of integers to what
will fit in a single byte, then you have your choice of Latin-1 or
several other system-dependent codes. If you restrict the range of
integers to what will fit in 7 bits, you have your choice of US-ASCII
or EBCDIC. (C provides support only for US-ASCII, via a "cast".)
No, C provides support for whatever the implementation and runtime
character sets are, which need not be US-ASCII. EBCDIC works just as
well, as does any character set which fulfils the requirements:

Section 5.2.1 "Character sets"

3 Both the basic source and basic execution character sets shall have
the following members: the 26 uppercase letters of the Latin
alphabet

A B C D E F G H I J K L M
N O P Q R S T U V W X Y Z

the 26 lowercase letters of the Latin alphabet

a b c d e f g h i j k l m
n o p q r s t u v w x y z

the 10 decimal digits

0 1 2 3 4 5 6 7 8 9

the following 29 graphic characters

! " # % & ' ( ) * + , - . / :
; < = > ? [ \ ] ^ _ { | } ~

the space character, and control characters representing horizontal
tab, vertical tab, and form feed. The representation of each member
of the source and execution basic character sets shall fit in a
byte. In both the source and execution basic character sets, the
value of each character after 0 in the above list of decimal digits
shall be one greater than the value of the previous. In source
files, there shall be some way of indicating the end of each line of
text; this International Standard treats such an end-of-line
indicator as if it were a single new-line character. In the basic
execution character set, there shall be control characters
representing alert, backspace, carriage return, and new line.
If you restrict the range of integers to 0 thru 35, you have extended
IBM-hexadecimal: 0123...9ABCDE...XYZ. If you restrict the range to
only 0 thru 15, C provides support via sprintf(&ch, "%x", intval).


If you restrict it to 0..63 you can use the Base64 encoding:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
0123456789+/

Chris C
Nov 15 '05 #31

This discussion thread is closed

Replies have been disabled for this discussion.