I am looking for help, i would like to know how can i use the endian.h
and
uint8_t* input = ...;
uint32_t v = input[0] | (input[1] << 8) | (input[2] << 16) | (input[3] <<
24);
This sort of arrangement generally gets the job done, no matter what which
of those endianess architechture you're on. It's more complicated issue if
you want to please this NG regulars, since:
- C++ doesn't guarantee that datatypes have precisely 8, 16 or 32 bits on
them. On those machines you have to do this at bit-level, most modern
architechtures support 8-bit chars and generally char,short,int,long, etc.
are multiplies of 8 bits.
Hmmm, so you just need to get typedefs right for uint8_t and uint32_t, and
rest should work out okay. Even if this NG regulars find this statement
fundamentally flawed, this (shifting to build up larger word) is what you
will find on most real-world UN*X sources for handling endianess.
The key is that you will have to know what endianess the stream/file is, so
that you can do the shifting of "input" in correct order. Some binary files
store values in big-endian (.lwo for example) and some in little-endian
(.3ds, for example). There is also BCP (?) endianess, which is rarely seen
in real-world since the platforms are novadays obsolete, so it's safe to
ignore those (again, stating this with NG regular disclaimer).