Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #29005

Re: Saving an unsigned short integer to string

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> (permalink)
Date Fri, 07 Dec 2012 00:37:35 -0500
From pete <pfiland@mindspring.com>
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

Show key headers only | View raw


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 <stdio.h>

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

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Saving an unsigned short integer to string Test <test@.nil.invalid.com> - 2012-12-06 17:24 +0200
  Re: Saving an unsigned short integer to string Eric Sosman <esosman@comcast-dot-net.invalid> - 2012-12-06 10:45 -0500
  Re: Saving an unsigned short integer to string Rui Maciel <rui.maciel@gmail.com> - 2012-12-06 15:58 +0000
    Re: Saving an unsigned short integer to string Rui Maciel <rui.maciel@gmail.com> - 2012-12-06 16:19 +0000
      Re: Saving an unsigned short integer to string Keith Thompson <kst-u@mib.org> - 2012-12-06 12:25 -0800
        Re: Saving an unsigned short integer to string Fred K <fred.l.kleinschmidt@gmail.com> - 2012-12-06 14:16 -0800
          Re: Saving an unsigned short integer to string Rui Maciel <rui.maciel@gmail.com> - 2012-12-06 23:23 +0000
  Re: Saving an unsigned short integer to string pete <pfiland@mindspring.com> - 2012-12-07 00:37 -0500
  Re: Saving an unsigned short integer to string Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-12-08 10:53 -0800

csiph-web