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

Message-ID <50C1809F.508C@mindspring.com> (permalink)
Date 2012-12-07 00:37 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: Saving an unsigned short integer to string
References <1qd1c8psp6ut5ipmhb7q86hbbjrmpcg5pf@4ax.com>

Show all headers | 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