问题描述
我发现这个code对左右。
I found this code on SO.
unsigned char * SerializeInt(unsigned char *buffer, int value)
{
/* Write big-endian int value into buffer; assumes 32-bit int and 8-bit char. */
buffer[0] = value >> 24;
buffer[1] = value >> 16;
buffer[2] = value >> 8;
buffer[3] = value;
return buffer + 4;
}
您可以看到code声称,它正在写一个整数到缓冲区,在Big Endian的方式。
You can see the code claims it is writing an integer into the buffer, in a Big Endian Way.
我的问题是:是否这个功能正常工作两个小endian和大端机器? 换句话说,在两个LE和BE机,它存储在大端方式来缓冲多少?如果是,为什么?
My question is: Does this function work correctly on both Little endian and Big endian machines? In other words, on both LE and BE machines, does it store the number to buffer in a Big Endian Way? And if yes, Why?
推荐答案
无论是系统的字节序,你的 SerializeInt
功能将存储价值
在阵列中的大端缓存
。因为它第一次写入最显著字节,存储为大端。请记住,当你评估值
它没有字节序的本身的,这就像任何数学的价值。
Whatever is the endianness of your system, your SerializeInt
function will store value
in big endian in the array buffer
. It is stored as big endian as it writes the most significant byte first. Remember that when you evaluate value
it has no endianness per se, it's like any mathematical value.
这篇关于转换的int char数组大端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!