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


Groups > comp.lang.c > #28987

Re: Saving an unsigned short integer to string

From Rui Maciel <rui.maciel@gmail.com>
Newsgroups comp.lang.c
Subject Re: Saving an unsigned short integer to string
Date 2012-12-06 15:58 +0000
Organization A noiseless patient Spider
Message-ID <k9qfav$ipa$1@dont-email.me> (permalink)
References <1qd1c8psp6ut5ipmhb7q86hbbjrmpcg5pf@4ax.com>

Show all headers | View raw


 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.

How about a good old fashion union?

<code>
#include <stdio.h>

union Foo 
{
        int i;
        char mytext[2];
};


int main(void)
{
        union Foo foo;

        foo.i = 55; 
        printf("foo.mytext: %c, %c\n", foo.mytext[0], foo.mytext[1]);
        return 0;
}
</code>


Rui Maciel

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