Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Thu, 06 Dec 2012 23:37:34 -0600 Message-ID: <50C1809F.508C@mindspring.com> Date: Fri, 07 Dec 2012 00:37:35 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: Saving an unsigned short integer to string References: <1qd1c8psp6ut5ipmhb7q86hbbjrmpcg5pf@4ax.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 79 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.156.228.79 X-Trace: sv3-ZK6D5uYttnQXPJzDUjkS426B0FmeOTOOi68RCQ0VrHwQ1jigMNzFo0qyJUhPcbovh6R6hjgjgCICbJ1!rwKML1qpqiezPR7TGvvo/EH9tL/TFUQiqSvw1nB8V7bfGb/v67dqqEEyoE+J9hSV+rMeH5MVfyw1!wg2/zn2D9A== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2297 X-Received-Bytes: 2438 Xref: csiph.com comp.lang.c:29005 Test wrote: > > I need to save ant interger with values ranging from 0-65535 > (0-FFFF) to a string and later retrieve the value. > > For example: > > .. > int i; > char mytxt[2]; > > i=55; > .. > > What would be the fastest way > to save i to mytxt and later retrieve it? > I am looking from a simple cast if possible. > I want to avoid using fuction calls. Get over it and use a function call anyway. /* BEGIN new.c output */ 55 65535 /* END new.c output */ /* BEGIN new.c */ #include void utoa(unsigned n, char *s); int main(void) { int i; char mytxt[sizeof "65535"]; puts("/* BEGIN new.c output */\n"); i = 55; utoa(i, mytxt); puts(mytxt); i = 65535; utoa(i, mytxt); puts(mytxt); puts("\n/* END new.c output */"); return 0; } void utoa(unsigned n, char *s) { unsigned tenth; char *p, swap; p = s; tenth = n; do { tenth /= 10; *p++ = (char)(n - 10 * tenth + '0'); } while ((n = tenth) != 0); *p-- = '\0'; while (p > s) { swap = *s; *s++ = *p; *p-- = swap; } } /* END new.c */ -- pete