* Byte ordering
@ 2009-09-04 12:04 Randi Botse
2009-09-04 12:57 ` Jonathan Nell
0 siblings, 1 reply; 2+ messages in thread
From: Randi Botse @ 2009-09-04 12:04 UTC (permalink / raw)
To: linux-c-programming
Hi all,
Given some codes:
...
unsigned int val = 0x0a0b0c0d;
unsigned char *str;
str = (unsigned char *) &val;
printf("%.2x %.2x %.2x %.2x\n", *str, *(str + 1), *(str + 2), *(str + 3));
...
the output is " 0d 0c 0b 0a "
the given codes try to divide a 32 bit integer into it's 8 bit nibbles.
with bit shifting and masking ( that you all teach me.... thanks u ;)
) i can get it correctly:
printf("%.2x %.2x %.2x %.2x\n", val >> 24 & 0xff, val >> 16 & 0xff,
val >> 8 & 0xff, val & 0xff)); /* 0a 0b 0c 0d */
but when address it with pointer to character i got it reversed, could
you please explain me why pointer to character reverse the ordering?
if what i need is only to reverse the ordering (pointer to character
index) to get a valid order? and if this a valid method?
- Randi
^ permalink raw reply [flat|nested] 2+ messages in thread* Re: Byte ordering
2009-09-04 12:04 Byte ordering Randi Botse
@ 2009-09-04 12:57 ` Jonathan Nell
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Nell @ 2009-09-04 12:57 UTC (permalink / raw)
To: Randi Botse; +Cc: linux-c-programming
2009/9/4 Randi Botse <nightdecoder@gmail.com>:
> Hi all,
>
> Given some codes:
>
> ...
> unsigned int val = 0x0a0b0c0d;
> unsigned char *str;
>
> str = (unsigned char *) &val;
> printf("%.2x %.2x %.2x %.2x\n", *str, *(str + 1), *(str + 2), *(str + 3));
> ...
>
> the output is " 0d 0c 0b 0a "
>
> the given codes try to divide a 32 bit integer into it's 8 bit nibbles.
> with bit shifting and masking ( that you all teach me.... thanks u ;)
> ) i can get it correctly:
>
> printf("%.2x %.2x %.2x %.2x\n", val >> 24 & 0xff, val >> 16 & 0xff,
> val >> 8 & 0xff, val & 0xff)); /* 0a 0b 0c 0d */
>
> but when address it with pointer to character i got it reversed, could
> you please explain me why pointer to character reverse the ordering?
> if what i need is only to reverse the ordering (pointer to character
> index) to get a valid order? and if this a valid method?
>
> - Randi
It's all to do with endianness and depends on the architecture you
have compiled for. x86 is little-endian, so your integer 0x0a0b0c0d is
actually stored as 0d0c0b0a in memory. So 'str', your pointer' is
actually pointing to the first byte in memory of the integer - i.e.
0x0d and incrementing the pointer will cause it to point to each byte
in turn as is stored in memory. So, if you want your code to be
portable across architectures with different endianness, you'll need
to use bit shifting as in your second example.
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2009-09-04 12:57 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-09-04 12:04 Byte ordering Randi Botse
2009-09-04 12:57 ` Jonathan Nell
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).