【发布时间】:2011-05-10 01:52:38
【问题描述】:
如何判断系统是大端还是小端?
【问题讨论】:
标签: endianness
如何判断系统是大端还是小端?
【问题讨论】:
标签: endianness
在 Rust 中(不需要 crates 或 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 crate 在内部所做的:https://docs.rs/byteorder/1.3.2/src/byteorder/lib.rs.html#1877
【讨论】:
在 Powershell 中
[System.BitConverter]::IsLittleEndian
【讨论】:
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
如果您愿意,代码是in a gist here。还有a bash command that you can run that will generate, compile, and clean up after itself。
【讨论】:
在 Rust 中(需要 byteorder crate):
use std::any::TypeId;
let is_little_endian = TypeId::of::<byteorder::NativeEndian>() == TypeId::of::<byteorder::LittleEndian>();
【讨论】:
在 C++20 中使用 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";
}
【讨论】:
在 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”。
【讨论】:
使用宏,
const int isBigEnd=1;
#define is_bigendian() ((*(char*)&isBigEnd) == 0)
【讨论】:
在 Python 中:
from sys import byteorder
print(byteorder)
# will print 'little' if little endian
【讨论】:
另一个使用联合的 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");
这与贝尔伍德使用的逻辑相同。
【讨论】:
在 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 */
【讨论】:
【讨论】:
(char)n == 1 吗?为什么我们必须获取地址,将其转换为char 指针,然后取消引用?这不会隐式完成(char)n 使用吗?
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";
}
【讨论】:
使用 Perl 的单行程序(几乎所有系统都应默认安装):
perl -e 'use Config; print $Config{byteorder}'
如果输出以 1(最低有效字节)开头,则它是 little-endian 系统。如果输出以更高的数字(最高有效字节)开头,则它是一个大端系统。请参阅Config 模块的文档。
【讨论】:
如果您使用的是 .NET:检查 BitConverter.IsLittleEndian 的值。
【讨论】: