Skip to content

Instantly share code, notes, and snippets.

@Hanaasagi
Created June 30, 2019 23:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hanaasagi/81be42270efa1bf9ec9ec5bd30359ea7 to your computer and use it in GitHub Desktop.
Save Hanaasagi/81be42270efa1bf9ec9ec5bd30359ea7 to your computer and use it in GitHub Desktop.
判断字节序,返回 true 为 Little-Endian 否则为 Big-Endian
int is_little_endian(void) {
union t
{
int a;
char b;
} c;
c.a = 1;
return c.b == 1;
}
// 如果为 big endian,存储为下(Hex表示,int 占 32 bit,char 占 8 bit)
// 0000 0001
// 所以以 char 读出时结果为 0
// 如果为 little endian,存储为下
// 0100 0000
// 所以以 char 读出时结果为 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment