Newsgroups: fj.lang.c
Path: galaxy.trc.rwcp.or.jp!coconuts.jaist!wnoc-tyo-news!foretune!lkbreth!merope!void
From: void@merope.pleiades.or.jp (Kusakabe Youichi)
Subject: Re: int->char change
Message-ID: <1994Sep1.175909.18770@merope.pleiades.or.jp>
Organization: Macintosh User Group PLEIADES
X-Newsreader: TIN [version 1.2 PL0]
References: <940829183121.M0120548@azalea1.jks.is.tsukuba.ac.jp> <kORjb.knakano@etl.go.jp> <CvGCxs.MoD@resgw.research.co.jp>
Date: Thu, 1 Sep 1994 17:59:09 GMT
Lines: 73
Xref: galaxy.trc.rwcp.or.jp fj.lang.c:1558
X-originally-archived-at: http://galaxy.rwcp.or.jp/text/cgi-bin/newsarticle2?ng=fj.lang.c&nb=1558&hd=a
X-reformat-date: Mon, 18 Oct 2004 15:18:22 +0900
X-reformat-comment: Tabs were expanded into 4 column tabstops by the Galaxy's archiver. See http://katsu.watanabe.name/ancientfj/galaxy-format.html for more info.

Shigeru Makino (mac@resgw.research.co.jp) wrote:
: #include <stdio.h>

: int
: itoa(buf, size, val)
: char                   *buf;
: int                     size;
: int                     val;
: {
:     int                     i, t;
:     char                   *p, *q;

:     size--;
:     for (p = buf, i = 1; i < size && val > 10; ++i) {
: t = val / 10;
: *p++ = val - (t * 10) + '0';
: val = t;
:     }

:     *(p + 1) = '\0';
:     if (val > 10) {
: return (0);
:     }
:     *p = val + '0';

:     for (q = buf; p > q; --p, ++q) {
: t = *q;
: *q = *p;
: *p = t;
:     }

:     return (i);
: }

$B$3$l$C$F!"0J2<$N$h$&$K=q$+$J$$$N$O$J$<$G$7$g$&(B?
($B=`Hw$7$?(Bbuf$B$N%5%$%:$r$"$U$l$k$h$j(B1$B2sA0$K(Breturn(0)$B$7$A$c$$$^$;$s$+(B?)

#include <stdio.h>

int itoa(buf, size, val)
char *buf;
int  size;
int  val;
{
int  i;

for (i = 0; i < size - 1 && val > 0; i++) {
buf[i] = val % 10 + '0';
val /= 10;
}
buf[i] = '\0';
if (val > 10)
return (0);
reverse(buf);
return (i);
}

reverse(buf)
char *buf;
{
int  i, j, t;

for (i = 0, j = strlen(buf) - 1; i < j; i++, j--) {
t = buf[j];
buf[j] = buf[i];
buf[i] = t;
}
}

  $B%X(B_$B%X(B   ----------------------------
$B%_!&!&(B $B%_(B  void@merope.pleiades.or.jp
 (     )$B!A(B          $B$/$5$+$Y$h$&$$$A(B
--------------------------------------
