方法一
echo -n I | od -o | head -n1 | cut -f2 -d" " | cut -c6
输出:1为小端模式,0为大端模式;
解析:od命令的作用为将指定内容以八进制、十进制、十六进制、浮点格式或ASCII编码字符方式显示;
方法二
echo -n I | od -o | head -n1 | awk '{print $2}'| cut -c6
输出:1为小端模式,0为大端模式;
解析:awk命令为文本处理。
方法三
lscpu | grep -i byte
输出:Byte Order: Little Endian;
解析:grep -i 为不区分大小写匹配;
注意:在低版本的Linux可能不支持lscpu命令。
方法四
dpkg-architecture | grep -i end
输出:
DEB_BUILD_ARCH_ENDIAN=little
DEB_HOST_ARCH_ENDIAN=little
DEB_TARGET_ARCH_ENDIAN=little
解析:dpkg-architecture命令是列出dpkg打包的一些环境参数;
方法五
static union { char c[4]; unsigned long mylong; } endian_test = {{ 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.mylong)
解析:内核自带判断方法
方法六
#include <stdio.h>
#include <arpa/inet.h>
#define ENDIANNESS (htons(1) == 1) ? "big endian" : "little endian"
int main()
{
printf("%s\n", ENDIANNESS);
return 0;
}
解析:通过网络字节序判断,网络字节序都是统一大端模式和原值判断来区分cpu工作模式