Byte ordering

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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
--
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Assembler]     [Git]     [Kernel List]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [C Programming]     [Yosemite Campsites]     [Yosemite News]     [GCC Help]

  Powered by Linux