2 バイト (short) 4 バイト (int) 語順を交換

exchangeBytes_16 (0x1234); 実行結果 0x3412exchangeBytes__32
(0x12345678); 実行結果 0x78563412

 /*******************************************************************************
 2 * Function Name  : exchangeBytes
 3 * Description    : 模拟的htons 或者 ntohs,如果系统支字节序更改可直接替换成系统函数
 4 * Input          : value
 5 * Output         : None
 6 * Return         : 更改过字节序的short数值
 7 * Attention           : None
 8 *******************************************************************************/
short exchangeBytes_16(short value)
{
    
    
	short	tmp_value;
	uint8_t	*index_1, *index_2;

	index_1 = (uint8_t *)&tmp_value;
	index_2 = (uint8_t *)&value;

	*index_1 = *(index_2+1);
	*(index_1+1) = *index_2;
	return tmp_value;
}
int exchangeBytes__32(int value)
{
    
    
    int    tmp_value;
	short  tmp_valueH,tmp_valueL;
	
	tmp_valueH=exchangeBytes_16((value>>16)&0xFFFF);//MSB
	tmp_valueL=exchangeBytes_16(value&0xFFFF);//LSB
	tmp_value=(tmp_valueH&0xFFFF)+((int)(tmp_valueL<<16)&0xFFFFFFFF);
	return tmp_value;	
}

2 つのエンディアンの交換の自己実装を参照してください。

おすすめ

転載: blog.csdn.net/weixin_49048045/article/details/122564320