C 函数说明(整数/字节数组)

C 函数说明(整数/字节数组),c,C,我不能理解这个函数。 你能帮帮我吗 void write_int(unsigned char *str, int value) { str[0] = (unsigned char)(value); str[1] = (unsigned char)(value >> 8); str[2] = (unsigned char)(value >> 16); str[3] = (unsigned char

我不能理解这个函数。 你能帮帮我吗

   void write_int(unsigned char *str, int value)
    {
        str[0] = (unsigned char)(value);
        str[1] = (unsigned char)(value >> 8);
        str[2] = (unsigned char)(value >> 16);
        str[3] = (unsigned char)(value >> 24);
    }

这将本质上把一个整数转换成一个字符数组

首先,
int
的大小通常为4字节(32位)

其次,
>
是位移位运算符,其中
a>>b
将数字
a
向右
b
移位几次<代码>>>>只需确保左侧插入了零即可

第三,当
int
被放入
char
中时,它会截断最高有效位。也就是说,我们只保留最小的8位


因此,此函数将最小的8位放入一个字符中,然后放入下一个8位,依此类推。

联机查找“位移位”它将
int
写入
str
所指向的地址,格式为。此函数可能要在big-endian平台上运行,该平台不支持little-endian。非常感谢,我将对此进行研究。值得注意的是,在big-endian平台上,OP发布的代码不仅仅是将
int
转换为
char
数组。它还反转,即以小尾端格式写入
int