如何检查系统是大端还是小端?
答案 0 :(得分:66)
答案 1 :(得分:25)
在Python中:
from sys import byteorder
print(byteorder)
答案 2 :(得分:10)
使用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");
这与贝尔伍德使用的逻辑相同。
答案 3 :(得分:7)
如果您使用的是.NET:请检查BitConverter.IsLittleEndian
。
答案 4 :(得分:3)
Perl的单行程序(默认情况下应该在几乎所有系统上安装):
perl -e 'use Config; print $Config{byteorder}'
如果输出以1(最低有效字节)开始,则它是一个小端系统。如果输出以更高的数字(最重要的字节)开始,则它是一个大端系统。请参阅Config模块的文档。
答案 5 :(得分:1)
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";
}
答案 6 :(得分:1)
在Rust中(需要字节顺序的条板箱):
Started by user admin
Running as SYSTEM
Building in workspace C:\Program Files (x86)\Jenkins210\workspace\ALMConnect
[ALMConnect] $ "C:\Program Files (x86)\Jenkins210\workspace\ALMConnect\HpToolsLauncher.exe" -paramfile props27022020181747552.txt
"Started..."
Timeout is set to: 300
Run mode is set to: RUN_REMOTE
Unable to retrieve test set folder: Node not found.
============================================================================
Starting test set execution
Test set name: Appiantesting TestSet, Test set id: 402
Class not registered
**Class not registered
Could not create scheduler, please verify ALM client installation on run machine by downloading and in installing the add-in form:** https://<server>.saas.microfocus.com/qcbin/TDConnectivity_index.html
Build step 'Execute Micro Focus functional tests from Micro Focus ALM' changed build result to FAILURE
Recording test results
RunResultRecorder: didn't find any test results to record
Finished: FAILURE
答案 7 :(得分:1)
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。
答案 8 :(得分:1)
在 Rust 中(不需要 crate 或 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
答案 9 :(得分:0)
在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 */
答案 10 :(得分:0)
使用宏,
const int isBigEnd=1;
#define is_bigendian() ((*(char*)&isBigEnd) == 0)
答案 11 :(得分: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”作为输出。
答案 12 :(得分:0)
在C ++ 20中,使用std::endian
:
Row
答案 13 :(得分:0)
答案 14 :(得分:0)
在 Powershell 中
[System.BitConverter]::IsLittleEndian