1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| /************************************************************************* > File Name: byte_order.c > Author:perrynzhou > Mail:perrynzhou@gmail.com > Created Time: Fri 15 Nov 2019 03:42:37 PM CST ************************************************************************/
#include <stdio.h> typedef union object_t { int a; char b; } object; enum { big_endian_type = 0, small_endian_type, }; int checkCpu(object *obj) { return obj->b == 1 ? small_endian_type : big_endian_type; } int main() { int v = 0x0001; object obj; obj.a = v; fprintf(stdout, "value:%d,value_string:%s,object address:%p\n", v, "0x0001", &obj); if (checkCpu(&obj) == big_endian_type) { fprintf(stdout, "store plan: |%d|%d|%d|%d|,big endian\n", 0, 0, 0, 1); } else { fprintf(stdout, "store plan: |%d|%d|%d|%d|,small endian\n", 1, 0, 0, 0); } return 0; }
|