Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

There's no itoa function

140 views
Skip to first unread message

Scott Burkett

unread,
May 9, 1995, 3:00:00 AM5/9/95
to
: There's a function called atoi but there's no itoa to convert an integer to a string. Is there another way to convert an integer to a string?!
: Btw, my unix operating system is AIX.

I believe itoa() was a Borland thing, but I could be wrong. Just use
sprintf():

char buf[MAX_BUFSIZE];

sprintf( buf, "%d", foo);

sprintf, sscanf(), et al. These guys are conversion lifesavers...

Regards.


--
Scott Burkett, sco...@intnet.net | Co-Author of the Linux Programmer's Guide
Computerpeople/DCI, Inc. | URL: http://www.bsn.usf.edu/~scottb
St. Petersburg, Florida, USA | Office: (813) 573-7003

Dozois Martin

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
Hi.

There's a function called atoi but there's no itoa to convert an integer to a string. Is there another way to convert an integer to a string?!
Btw, my unix operating system is AIX.

--
------------------------------
Martin Dozois
Departement de communication
Universite de Montreal

david_peter

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
In article <dozoism....@mistral.ERE.UMontreal.CA>
You can convert an integer to a string using sprintf(3S).
--
_/_/_/ _/_/_/_/ _/_/_/_/ David P Peter
_/ _/ _/ _/ _/ _/ Insignia Solutions Inc.
_/ _/ _/_/_/_/ _/_/_/_/ da...@isltd.insignia.com
_/ _/ _/ _/ +44 1494 453431
_/_/_/ _/ _/ In God We Trust (In Cars We Rust)

Doug Royer [KD6HBW]

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
sprintf(string, "%d", integer);

In article <dozoism....@mistral.ERE.UMontreal.CA>, doz...@ERE.UMontreal.CA (Dozois Martin) writes:
> Hi.
>
> There's a function called atoi but there's no itoa to convert an integer to a string. Is there another way to convert an integer to a string?!
> Btw, my unix operating system is AIX.
>
> --
> ------------------------------
> Martin Dozois
> Departement de communication
> Universite de Montreal

--

< IF YOUR FORWARD THIS PLEASE DELETE MY E-MAIL ADDRESS (above) >
< AND SIGNATURE (below) I DO NOT WORK FOR SUPPORT - THANKS >
------------------------------------------------------------------------------
Doug Royer - KD6HBW, Doug....@Eng.Sun.COM, (415)336-6503
SunSoft,2550 Garcia Ave,Mountain View, CA 94043 MS: MTV21-122

Toby Chappell

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
Mike Herman (mi...@hernix.org) wrote:
: char I_T_O_A[32];
: char *itoa(num, siz)
: int num, siz;
: { register int x, y;
: x = 31;
: I_T_O_A[x--] = '\0';
: if(num == 0)
: I_T_O_A[x--] = '0';
: else
: { while(num)
: {
: I_T_O_A[x] = (num % 10) + '0';
: num /= 10;
: x--;
: }
: }
: x++;
: while(strlen(&I_T_O_A[x]) < siz)
: I_T_O_A[--x] = '0';
: return(&I_T_O_A[x]);
: }


Why people continue to advocate hacks like this is beyond me....
there is nothing wrong from either a portability or efficiency
point of view from just using sprintf().


char str[32];
sprintf (str, "%d", some_integer);


2 lines of code versus 20 or so. you make the call....

--
"I am as firmly convinced that religions do harm as I am that they
are untrue." - Bertrand Russell, preface to _Why_I_am_Not_A_Christian_.
=======================================================================
http://lexus.gtri.gatech.edu/~tchappel

Toby Chappell

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
Dave Frascone (da...@glenatl.glenayre.com) wrote:
: Toby Chappell (tcha...@lexus.gtri.gatech.edu) wrote:
: : Why people continue to advocate hacks like this is beyond me....

: : there is nothing wrong from either a portability or efficiency
: : point of view from just using sprintf().

: : char str[32];
: : sprintf (str, "%d", some_integer);

: sprintf is fat and slow. That's one of my major problems with it.
: But, if you are not inside of a loop, why not...


Maybe you need a new implementation of sprintf...
Compared to the hack I replied to, sprintf is MUCH faster.
Running it 10000 times in a loop gave (using the time command;
it is rudimentary, but still gives a fairly reliable ballpark figure)

('bar' is the implementation using sprintf 10000 times in a loop;
'foo' is the previously mentioned 'itoa' hack)

/home/tchappel/c> time ./bar 5

real 0m0.30s
user 0m0.15s
sys 0m0.03s
/home/tchappel/c> time ./foo 5

real 0m2.05s
user 0m1.83s
sys 0m0.03s
/home/tchappel//c>


looks pretty clear to me which is faster....

Kevin A. Archie

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
da...@glenatl.glenayre.com (Dave Frascone) writes:

>Toby Chappell (tcha...@lexus.gtri.gatech.edu) wrote:

>: there is nothing wrong from either a portability or efficiency
>: point of view from just using sprintf().

>sprintf is fat and slow. That's one of my major problems with it.


>But, if you are not inside of a loop, why not...

Yes, but if you're converting an int to ASCII, chances are you're
putting together some output, and the I/O is going to be *much* slower
than your preparation. Use sprintf. Once you're done, if your
profiler tells you all your cycles are being sucked by sprintf, *then*
start looking for optimizations. Life is too short to spend it writing
code to do things that the C library does for you.

- Kevin
--
Kevin Archie http://alumni.caltech.edu/~karchie
Software Engineer (but not spokesman) kar...@alumni.caltech.edu
MIL 3, Inc. (so this is my opinion, not theirs) kar...@mil3.com

Mike Herman

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
Use this:


char I_T_O_A[32];
char *itoa(num, siz)
int num, siz;
{ register int x, y;
x = 31;
I_T_O_A[x--] = '\0';
if(num == 0)
I_T_O_A[x--] = '0';
else
{ while(num)
{
I_T_O_A[x] = (num % 10) + '0';
num /= 10;
x--;
}
}
x++;
while(strlen(&I_T_O_A[x]) < siz)
I_T_O_A[--x] = '0';
return(&I_T_O_A[x]);
}

--
--------------------------------------------------------------------------
Mike Herman mi...@hernix.org hernix!mike
Hernix provides low volume email and UUCP feeds in the Cleveland area.
--------------------------------------------------------------------------

Dave Frascone

unread,
May 10, 1995, 3:00:00 AM5/10/95
to
Toby Chappell (tcha...@lexus.gtri.gatech.edu) wrote:

: Why people continue to advocate hacks like this is beyond me....

: there is nothing wrong from either a portability or efficiency
: point of view from just using sprintf().

: char str[32];
: sprintf (str, "%d", some_integer);

sprintf is fat and slow. That's one of my major problems with it.

But, if you are not inside of a loop, why not...


Later,


Dave

Patrick Horgan

unread,
May 11, 1995, 3:00:00 AM5/11/95
to
Here's an overloaded itoa() in C++ that let's you use his static buffer,
or provide a pointer and size of yours. <snicker;>

#include <strstream.h>
itoa(int val)
{
static char numbuffer[257];
ostrstream intbuf(numbuffer,257);
intbuf << val << ends;
return &numbuffer[0];
}

itoa(int val,char *inoutbuf,int bufsize)
{
ostrstream intbuf(inoutbuf,bufsize);
intbuf << val << ends;
return &numbuffer[0];
}

Patrick

p.s. How many people besides me are doing systems programming in C++ by
preference these days. I only write C if I'm writing examples for
others that just don't know C++ yet. I find fewer and fewer of
those.

--
_______________________________________________________________________
/ These opinions are mine, and not Amdahl's (except by coincidence;). \
| (\ |
| Patrick J. Horgan Amdahl Corporation \\ Have |
| pat...@amdahl.com 1250 East Arques Avenue \\ _ Sword |
| Phone : (408)992-2779 P.O. Box 3470 M/S 316 \\/ Will |
| FAX : (408)773-0833 Sunnyvale, CA 94088-3470 _/\\ Travel |
\___________________________O16-2294________________________\)__________/

Ferdinand de Boevere

unread,
May 11, 1995, 3:00:00 AM5/11/95
to
In article <D8D17...@hernix.org>, Mike Herman <mi...@hernix.org> wrote:
>Use this:
>
>
>char I_T_O_A[32];
>char *itoa(num, siz)
>int num, siz;
>{ register int x, y;
> x = 31;
> I_T_O_A[x--] = '\0';
> if(num == 0)
> I_T_O_A[x--] = '0';
> else
> { while(num)
> {
> I_T_O_A[x] = (num % 10) + '0';
> num /= 10;
> x--;
> }
> }
> x++;
> while(strlen(&I_T_O_A[x]) < siz)
> I_T_O_A[--x] = '0';
> return(&I_T_O_A[x]);
>}

or even this:

void itoa(int n, char *s) /* take care of s' size big enough */
{
static int i;

if (n / 10)
itoa (n / 10, s);
s[i++] = n % 10 + '0';
if (n < 10)
s[i] = '\0';
}


Ferdinand ,


Darrell Grainger

unread,
May 12, 1995, 3:00:00 AM5/12/95
to
Mike Herman (mi...@hernix.org) wrote:
: Use this:


: char I_T_O_A[32];
: char *itoa(num, siz)
: int num, siz;
: { register int x, y;
: x = 31;
: I_T_O_A[x--] = '\0';
: if(num == 0)
: I_T_O_A[x--] = '0';
: else
: { while(num)
: {
: I_T_O_A[x] = (num % 10) + '0';
: num /= 10;
: x--;
: }
: }
: x++;
: while(strlen(&I_T_O_A[x]) < siz)
: I_T_O_A[--x] = '0';
: return(&I_T_O_A[x]);

: }

Why not just use:

char s[20]; /* I assume 20 is enough to hold the number */
int number;

/* initialize number here */

sprintf(s, "%d", number); /* same as itoa() */

: --

: --------------------------------------------------------------------------
: Mike Herman mi...@hernix.org hernix!mike
: Hernix provides low volume email and UUCP feeds in the Cleveland area.
: --------------------------------------------------------------------------

--
Darrell


Trey Murff

unread,
May 12, 1995, 3:00:00 AM5/12/95
to
Dozois Martin (doz...@ERE.UMontreal.CA) wrote:
: Hi.

: There's a function called atoi but there's no itoa to convert an integer to a string. Is there another way to convert an integer to a string?!
: Btw, my unix operating system is AIX.

You can allways try
sprintf(buff,fmt,arg, ....)

Bruce Korb

unread,
May 12, 1995, 3:00:00 AM5/12/95
to
tcha...@lexus.gtri.gatech.edu (Toby Chappell) writes:

>Mike Herman (mi...@hernix.org) wrote:

[ hack deleted ]

> Why people continue to advocate hacks like this is beyond me....
>there is nothing wrong from either a portability or efficiency
>point of view from just using sprintf().

I find it handy when writing boot code where you cannot
afford the space required of the printf formatting library.
Otherwise, I would agree with you.
--
Bruce Korb | Microport, Inc.
bk...@mport.com | 108 Whispering Pines Dr.
(408) 438-8649 | Scotts Valley, CA 95066

Paul Dowman

unread,
May 15, 1995, 3:00:00 AM5/15/95
to
>> Why people continue to advocate hacks like this is beyond me....
>>there is nothing wrong from either a portability or efficiency
>>point of view from just using sprintf().

Then why is there an atoi, when you could just use scanf()?

I just wonder why there's one but not the other?!

Paul.


Kazimir Kylheku

unread,
May 16, 1995, 3:00:00 AM5/16/95
to
In article <3oqpqc$s...@mordred.gatech.edu>,
Toby Chappell <tcha...@lexus.gtri.gatech.edu> wrote:

>Mike Herman (mi...@hernix.org) wrote:
>: char I_T_O_A[32];
>: char *itoa(num, siz)
>: int num, siz;

>there is nothing wrong from either a portability or efficiency
>point of view from just using sprintf().

Well, it's not that much of a hack. It seems fairly portable.


>
>char str[32];
>sprintf (str, "%d", some_integer);
>
>

>2 lines of code versus 20 or so. you make the call....

I'd tend to agree with that. Where the standard library is available,
it's unwise to not use it. I'd disagree with the efficiency argument.
Scanf() has to parse its format string each time you call it, and has to
deal with many contingencies based on all the possible options you can
pass to it.


I once wrote a log-file scanner using scanf(). It took 20 seconds to
process a 350K file.

I then re-wrote it using Flex (The GNU lexical-analyzer generator
that's backward compatible with Lex). In addition, I drove it with
Yacc.

The resulting program munched through a 4 megabyte file in about the
same elapsed time, while also doing a ton of stuff that the old
program didn't, such as depositing chargeable transactions to a B+
tree database file, and ensuring that identical transactions made by
the same user within 12 hours after the first occurence are recorded
as one purchase.

I was somewhat pleased, to say the least.
--
"... nobody really knows what the Bourne shell's grammar is. Even examination
of the source code is little help. "
-Tom Duff, "Rc -- A Shell for Plan 9 and UNIX Systems"

Niranjan Perera

unread,
May 18, 1995, 3:00:00 AM5/18/95
to
In article <3p8h8h$a...@granite.sentex.net>, pdo...@uoguelph.ca says...

>
>>> Why people continue to advocate hacks like this is beyond me....
>>>there is nothing wrong from either a portability or efficiency
>>>point of view from just using sprintf().
>
>Then why is there an atoi, when you could just use scanf()?
>
>I just wonder why there's one but not the other?!
>
>Paul.
>

The K&R book has it.
--
--
"Unix is simple, but it takes a guru to understand its simplicity." - ?


Warren Gay VE3WWG

unread,
May 19, 1995, 3:00:00 AM5/19/95
to
Niranjan Perera (niranja...@hboc.com) wrote:
: In article <3p8h8h$a...@granite.sentex.net>, pdo...@uoguelph.ca says...

: >>> Why people continue to advocate hacks like this is beyond me....
: >>>there is nothing wrong from either a portability or efficiency
: >>>point of view from just using sprintf().
: >
: >Then why is there an atoi, when you could just use scanf()?
: >
: >I just wonder why there's one but not the other?!
: >
: >Paul.

: The K&R book has it.

Function scanf has to hack through the format string first, before it
knows what calls to make. This is why it is avoided. It also, drags in a
lot of other baggage from the library, which you may not want, depending
on what you are writing.

atoi() ends up translating into a strtol() call anyhow (at least on
SysV, as I generally plead ignorance of BSD matters :). In fact, on some
systems, it might just be a macro call:

#define atoi(s) (int)(strtol(s,NULL,10))
#define atol(s) (strtol(s,NULL,10))

When implemented as a macro, you will obviously have to include the
correct header file to get it =)

strtol() is the preferred call, since by careful use of this call, you
know when conversion errors have occurred, and when all input in the
string contributed to the value. Note that:

i = atoi("23abc");

will happily return 23 without complaint, whereas if you check the
returned values from strtol(), you will know that the conversion stopped
at the 'a'.

--------------------
Warren W. Gay VE3WWG John Coutts Library Services Limited
w...@wizbang.COUTTS.ON.CA Niagara Falls, Ontario, Canada

0 new messages