Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #28985 > unrolled thread
| Started by | Test <test@.nil.invalid.com> |
|---|---|
| First post | 2012-12-06 17:24 +0200 |
| Last post | 2012-12-08 10:53 -0800 |
| Articles | 9 — 7 participants |
Back to article view | Back to comp.lang.c
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
| From | Test <test@.nil.invalid.com> |
|---|---|
| Date | 2012-12-06 17:24 +0200 |
| Subject | Saving an unsigned short integer to string |
| Message-ID | <1qd1c8psp6ut5ipmhb7q86hbbjrmpcg5pf@4ax.com> |
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.
[toc] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2012-12-06 10:45 -0500 |
| Message-ID | <k9qein$dts$1@dont-email.me> |
| In reply to | #28985 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rui Maciel <rui.maciel@gmail.com> |
|---|---|
| Date | 2012-12-06 15:58 +0000 |
| Message-ID | <k9qfav$ipa$1@dont-email.me> |
| In reply to | #28985 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rui Maciel <rui.maciel@gmail.com> |
|---|---|
| Date | 2012-12-06 16:19 +0000 |
| Message-ID | <k9qgip$ohe$1@dont-email.me> |
| In reply to | #28987 |
Rui Maciel wrote:
> union Foo
> {
> int i;
> char mytext[2];
> };
It must be said that it isn't a good idea to use the char and int data type,
because the number of bits that are used to define those types are platform-
dependent.
Considering the range that was mentioned, which was 0-65535, the following
union might be more appropriate:
union Foo
{
uint16_t i;
uint8_t mytext[2];
};
Rui Maciel
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2012-12-06 12:25 -0800 |
| Message-ID | <lnk3svorlw.fsf@nuthaus.mib.org> |
| In reply to | #28988 |
Rui Maciel <rui.maciel@gmail.com> writes:
> Rui Maciel wrote:
>> union Foo
>> {
>> int i;
>> char mytext[2];
>> };
>
> It must be said that it isn't a good idea to use the char and int data type,
> because the number of bits that are used to define those types are platform-
> dependent.
>
> Considering the range that was mentioned, which was 0-65535, the following
> union might be more appropriate:
>
> union Foo
> {
> uint16_t i;
> uint8_t mytext[2];
> };
That's certainly better than using int, but byte ordering varies from
one system to another. Storing a value in i could result in the
high-order byte being stored in mytext[0] and the low-order byte in
mytext[1], or vice versa. If you want to be able to share the byte
values across different systems, you'll want to control the byte order
yourself -- which is why we have "network byte order".
See http://en.wikipedia.org/wiki/Endianness
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Fred K <fred.l.kleinschmidt@gmail.com> |
|---|---|
| Date | 2012-12-06 14:16 -0800 |
| Message-ID | <433bb081-3e4f-4c02-96cd-994654a42ace@googlegroups.com> |
| In reply to | #28994 |
On Thursday, December 6, 2012 12:25:31 PM UTC-8, Keith Thompson wrote:
> Rui Maciel <rui.maciel@gmail.com> writes: > Rui Maciel wrote: >> union Foo >> { >> int i; >> char mytext[2]; >> }; > > It must be said that it isn't a good idea to use the char and int data type, > because the number of bits that are used to define those types are platform- > dependent. > > Considering the range that was mentioned, which was 0-65535, the following > union might be more appropriate: > > union Foo > { > uint16_t i; > uint8_t mytext[2]; > }; That's certainly better than using int, but byte ordering varies from one system to another. Storing a value in i could result in the high-order byte being stored in mytext[0] and the low-order byte in mytext[1], or vice versa. If you want to be able to share the byte values across different systems, you'll want to control the byte order yourself -- which is why we have "network byte order". See http://en.wikipedia.org/wiki/Endianness
These solutions assume that a byte on the OP's platform is eight bits. While this is almost always true today, ther are platforms where this is not so.
The important thing is that 16 bits are needed to store any value in the specified range. And yes, byte-order will also be of concern if the store and restore operations are on different platforms even if the size of a byte is the same on both.
--
Fred K
[toc] | [prev] | [next] | [standalone]
| From | Rui Maciel <rui.maciel@gmail.com> |
|---|---|
| Date | 2012-12-06 23:23 +0000 |
| Message-ID | <k9r9cs$u9p$1@dont-email.me> |
| In reply to | #28999 |
Fred K wrote: > These solutions assume that a byte on the OP's platform is eight bits. > While this is almost always true today, ther are platforms where this is > not so. The standard states that the intN_t types have exactly N bits, without padding, and if an implementation provides types with N bits then it must define those types. If the implementation doesn't provide those types then those types are considered optional, and the implementation doesn't need to provide them. Therefore, when we are dealing with a platform which doesn't support these types, those types won't exist, and that code won't compile. That isn't exactly a problem. It's instead a moment to celebrate, because finally someone actually found one of those mythical platforms where it was impossible to implement a 8-bit data type. Rui Maciel
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-12-07 00:37 -0500 |
| Message-ID | <50C1809F.508C@mindspring.com> |
| In reply to | #28985 |
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
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2012-12-08 10:53 -0800 |
| Message-ID | <963346fe-900b-41aa-8c86-027c3abb9999@googlegroups.com> |
| In reply to | #28985 |
On Thursday, December 6, 2012 3:24:20 PM UTC, Test wrote: > I need to save ant interger with values ranging from 0-65535 (0-FFFF) to a > string and later retrieve the value. > 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. Read Eric Sosman on strings and buffers. Assuming you really want a buffer make mytxt unsigned char. then you want mytxt[0] = ((i >> 8) & 0xFF); mytxt[1] = (i & 0xFF); Modern compilers will usually optimise these sorts of operations out. But even if it doesn't, logical operations are so fast that it's highly unlikely to be a rate-limiting step in your program.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web