Skip to content

Instantly share code, notes, and snippets.

@cgaebel
Created March 18, 2011 16:53
Show Gist options
  • Select an option

  • Save cgaebel/876406 to your computer and use it in GitHub Desktop.

Select an option

Save cgaebel/876406 to your computer and use it in GitHub Desktop.
/* 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