Monday, June 24, 2013

Byte Order Fallacy

http://commandcenter.blogspot.fr/2012/04/byte-order-fallacy.html

TL;DR:

Let's say your data stream has a little-endian-encoded 32-bit integer. Here's how to extract it (assuming unsigned bytes):
i = (data[0]<<0) | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
If it's big-endian, here's how to extract it:
i = (data[3]<<0) | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);
Both these snippets work on any machine.

No comments:

Post a Comment