如何检查系统是大端还是小端?
在Python中:
from sys import byteorder
print(byteorder)
# will print 'little' if little endian
另一个使用 union 的 C 代码
union {
int i;
char c[sizeof(int)];
} x;
x.i = 1;
if(x.c[0] == 1)
printf("little-endian\n");
else printf("big-endian\n");
贝尔伍德使用的逻辑相同。
std::endian
:
#include <bit>
#include <iostream>
int main() {
if constexpr (std::endian::native == std::endian::little)
std::cout << "little-endian";
else if constexpr (std::endian::native == std::endian::big)
std::cout << "big-endian";
else
std::cout << "mixed-endian";
}
Perl 的单行程序(几乎所有系统上都应该默认安装):
perl -e 'use Config; print $Config{byteorder}'
如果输出以 1(最低有效字节)开头,则它是小端系统。如果输出以较高的数字(最高有效字节)开始,则它是大端系统。请参阅 Config 模块的文档。
如果您使用 .NET:检查
BitConverter.IsLittleEndian
的值。
在 Rust 中(不需要板条箱或
use
声明)
在函数体内:
if cfg!(target_endian = "big") {
println!("Big endian");
} else {
println!("Little endian");
}
函数体之外:
#[cfg(target_endian = "big")]
fn print_endian() {
println!("Big endian")
}
#[cfg(target_endian = "little")]
fn print_endian() {
println!("Little endian")
}
这就是
byteorder
板条箱的内部功能:https://docs.rs/byteorder/1.3.2/src/byteorder/lib.rs.html#1877
在 Powershell 中
[System.BitConverter]::IsLittleEndian
在 Linux 中,
static union { char c[4]; unsigned long mylong; } endian_test = { { 'l', '?', '?', 'b' } };
#define ENDIANNESS ((char)endian_test.mylong)
if (ENDIANNESS == 'l') /* little endian */
if (ENDIANNESS == 'b') /* big endian */
C++ 解决方案:
namespace sys {
const unsigned one = 1U;
inline bool little_endian()
{
return reinterpret_cast<const char*>(&one) + sizeof(unsigned) - 1;
}
inline bool big_endian()
{
return !little_endian();
}
} // sys
int main()
{
if(sys::little_endian())
std::cout << "little";
}
在 Rust 中(需要字节顺序箱):
use std::any::TypeId;
let is_little_endian = TypeId::of::<byteorder::NativeEndian>() == TypeId::of::<byteorder::LittleEndian>();
使用宏,
const int isBigEnd=1;
#define is_bigendian() ((*(char*)&isBigEnd) == 0)
C 语言
#include <stdio.h>
/* function to show bytes in memory, from location start to start+n*/
void show_mem_rep(char *start, int n)
{
int i;
for (i = 0; i < n; i++)
printf("%2x ", start[i]);
printf("\n");
}
/*Main function to call above function for 0x01234567*/
int main()
{
int i = 0x01234567;
show_mem_rep((char *)&i, sizeof(i));
return 0;
}
当上面的程序在小端机器上运行时,给出“67 45 23 01”作为输出,而如果它在大端机器上运行,给出“01 23 45 67”作为输出。
n00bs 最佳答案的可编译版本:
#include <stdio.h>
int main() {
int n = 1;
// little endian if true
if(*(char *)&n == 1) {
printf("Little endian\n");
} else {
printf("Big endian\n");
}
}
将其粘贴到
check-endianness.c
中并编译并运行:
$ gcc -o check-endianness check-endianness.c
$ ./check-endianness
整个命令是一个可复制/粘贴的 bash 脚本,您可以将其粘贴到终端中:
cat << EOF > check-endianness.c
#include <stdio.h>
int main() {
int n = 1;
// little endian if true
if(*(char *)&n == 1) {
printf("Little endian\n");
} else {
printf("Big endian\n");
}
}
EOF
gcc -o check-endianness check-endianness.c \
&& ./check-endianness \
&& rm check-endianness check-endianness.c
如果您愿意,代码是这里的要点。还有 一个您可以运行的 bash 命令,它将生成、编译并自行清理。
在 bash 中(来自如何判断 Linux 系统是大端还是小端?):
endian=`echo -n "I" | od -to2 | head -n1 | cut -f2 -d" " | cut -c6`
if [ "$endian" == "1" ]; then
echo "little-endian"
else
echo "big-endian"
fi
C 逻辑检查您的处理器是遵循小端还是大端
unsigned int i =12345;
char *c = (char *)&i; // typecast int to char* so that it points to first bit of int
if(*c != 0){ // If *c points to 0 then it is Big endian else Little endian
printf("Little endian");
}
else{
printf("Big endian");
}
希望这有帮助。这是我在面试中提出的关于嵌入式软件工程师角色的问题之一
在 C 和 GCC 9.4 中:
#include <endian.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
...
#endif
注意粗心者的陷阱:不要这样做:
#ifdef __LITTLE_ENDIAN
因为__LITTLE_ENDIAN和__BIG_ENDIAN都是在endian.h中定义的!还有 LITTLE_ENDIAN / BIG_ENDIAN (未定义前导下划线)
如果你想在 Linux 机器上检查它,你可以使用下面提到的 cmd
lscpu | grep "Byte Order"
运行此命令后,您将得到如下所示的输出
Byte Order: Little Endian
希望有帮助