Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将数据从int复制到2个无符号字符_C_Int_Unsigned Char - Fatal编程技术网

C 将数据从int复制到2个无符号字符

C 将数据从int复制到2个无符号字符,c,int,unsigned-char,C,Int,Unsigned Char,如何将数据从一个int(int-port1=52010)复制到一对无符号字符(unsigned char-port2[2])?我不知道如何处理除法。通常使用掩蔽和移位 const unsigned short port = 52010; uint8_t port2[2]; 大端语: port2[0] = port >> 8; port2[1] = port & 255; 小恩迪亚: port2[0] = port & 255; port2[1] = port &g

如何将数据从一个int(int-port1=52010)复制到一对无符号字符(
unsigned char-port2[2]
)?我不知道如何处理除法。

通常使用掩蔽和移位

const unsigned short port = 52010;
uint8_t port2[2];
大端语:

port2[0] = port >> 8;
port2[1] = port & 255;
小恩迪亚:

port2[0] = port & 255;
port2[1] = port >> 8;
对于IP网络中使用的端口号之类的内容,您通常会使用所谓的“网络字节顺序”(也称为“big-endian”),并且有一个特殊的宏用于执行此操作:

const unsigned short port_n = ntohs(port);

请注意,这会将端口号保持为无符号短端口,同时在必要时交换字节。

通常使用掩蔽和移位

const unsigned short port = 52010;
uint8_t port2[2];
    port2[0] = port1 >> 8;
    port2[1] = port1 & 0x00FF;
大端语:

port2[0] = port >> 8;
port2[1] = port & 255;
小恩迪亚:

port2[0] = port & 255;
port2[1] = port >> 8;
对于IP网络中使用的端口号之类的内容,您通常会使用所谓的“网络字节顺序”(也称为“big-endian”),并且有一个特殊的宏用于执行此操作:

const unsigned short port_n = ntohs(port);
请注意,这会将端口号保持为
无符号短
,同时在必要时交换字节

    port2[0] = port1 >> 8;
    port2[1] = port1 & 0x00FF;
或者根据端点的不同按相反顺序排列


或者根据端部的不同,按相反的顺序排列。

嗯,完全正确@展开,固定。嗯,完全正确@展开,固定。