Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Saving an unsigned short integer to string |
| Date | 2012-12-06 10:45 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <k9qein$dts$1@dont-email.me> (permalink) |
| References | <1qd1c8psp6ut5ipmhb7q86hbbjrmpcg5pf@4ax.com> |
On 12/6/2012 10:24 AM, 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.
Nomenclature: What you describe is not a "string" as C uses
the term. In C, a "string" is an array of `char' containing zero
or more non-zero "payload" characters followed by one zero byte
to mark the end. You've got an array of `char', but it's not a
"string" unless by coincidence.
Moving on: The first thing you should do is change `char' to
`unsigned char', avoiding potential mysteries on systems where
`char' is a signed type. An `unsigned char' can hold values from
0 through 255 (possibly higher, but that's rare), but a plain
`char' might go only as high as 127. And then:
int i = ... something between 0 and 65535 ...
unsigned char mybytes[2]; // `mytxt' is a misleading name
mybytes[0] = i & 0xff;
mybytes[1] = i >> 8;
...
int j = (mybytes[1] << 8) + mybytes[0];
(You could reverse the positions of the low- and high-order bytes,
if you like, so long as the encoding and decoding use the same
convention.)
There are no guarantees about speed, but one approach that
is likely to be faster is
int i = ... something between 0 and 65535 ...
unsigned short mybytes;
mybytes = i;
...
int j = mybytes;
Whether you can use this depends on whether you truly need a
two-character array; you haven't explained that requirement.
--
Eric Sosman
esosman@comcast-dot-net.invalid
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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