convert int to char

K

Keith Thompson

Chris Croughton said:
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.
 
C

CBFalconer

Christopher said:
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.
 
C

Christopher Benson-Manica

CBFalconer said:
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...
 
C

Chris Croughton

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!".
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
 
K

Kenny McCormack

Chris Croughton wrote on 11/04/05 :

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

Robert Maas, see http://tinyurl.com/uh3t

From: "John Carroll said:
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).
 
A

Anonymous 7843

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
 
S

SM Ryan

(e-mail address removed) (Robert Maas, see http://tinyurl.com/uh3t) wrote:
# > From: "John Carroll" <[email protected]>
# > 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.
 
W

Walter Roberson

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"
 
C

Chris Croughton

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 said:
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
 

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

No members online now.

Forum statistics

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

Latest Threads

Top