Created
March 18, 2011 16:53
-
-
Save cgaebel/876406 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* are we running on a little-endian system? */ | |
| static inline int little_endian() | |
| { | |
| union { | |
| uint16_t i; | |
| char c[2]; | |
| } t = { 0x0001 }; | |
| return *t.c == 1; | |
| } | |
| static inline void* swap_bytes(void* s, size_t len) | |
| { | |
| for(char* b = s, | |
| * e = b + len - 1; | |
| b < e; | |
| b++, e--) | |
| { | |
| char t = *b; | |
| *b = *e; | |
| *e = t; | |
| } | |
| return s; | |
| } | |
| /* big endian to native endian. works in-place */ | |
| static inline void* be2ne(void* s, size_t len) | |
| { | |
| return little_endian() ? swap_bytes(s, len) : s; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment