我已经阅读了这段 C 格式的代码,但我不明白具体的过程是什么。谁能用java为我解释一下,或者如果java中有更好的方法,你能在这里写下来吗?谢谢
bool endianess(){
int testNum;
char *ptr;
testNum=1;
ptr=(char*) &testNum;
return (*ptr); //* return the byte at the lowest address
}
最佳答案
在 Little Endian 架构中,整数值 0x1 将像
一样被写到内存中0x1 0x0 0x0 ....
这样该函数将返回 1
相反,在 Big Endian 架构中,字节顺序为
0x0 0x0 ... 0x1
但是 int 中有多少字节(大于或等于 2 才能工作),所以
该函数将返回 0。
这里有一个引用说明为什么会这样 can't be done in Java directly ,但您可以使用 JNI转义到 C 库并返回结果。
关于java - 用java编写一个判断计算机是大端还是小端的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21447526/