c++ - storing integer value in reverse order -
while printing hexadecimal value(the value stored in a) printing in reverse order ,
int main() { int i; uint8_t b[4]; int = 0xaabbccdd; uint8_t *ptr; ptr = &b; memcpy(ptr,&a,4 * sizeof(uint8_t)); for(i = 0;i < 4;i++) { printf("%x ",*ptr++); } return(0); }
output
dd cc bb aa
how store in same order gave input(aabbccdd)
this has endianness, it's how computers store numerical values in memory.
to sum up, modern processors (x86, x86-64, arm) little-endian (arm bi-endian now, can configure in hardware).
what means least significant bytes have lowest address (little end first).
instead of trying go against this, i'd advise work around if need to.
one thing do, if really have to, use network byte order, defined big-endian. functions htons(), htonl(), ntohs(), ntohl()
helpful.
Comments
Post a Comment