我想通过网络发送一些双精度浮点数。(标准 C,标准套接字)没有 htond 或 ntohd 将数据与网络字节顺序进行转换。我该怎么办?我脑子里有几个解决方案,但我想知道常见的做法是什么。
(我也想知道发送 64 位整数的常见做法是什么,比如 gstreamer 使用的 gint64 值)
编辑:这是我想到的一种解决方案。我认为它适用于任何大小的整数,但它适用于双精度数吗?
void swap_if_necessary (void* buff, int buff_len)
{
uint32_t foo = 1;
if ( htonl(foo) != foo )
{
char* to_swap = (char*)buff;
int i;
for (i = 0; i < buff_len/2; i++)
{
char swap_buff = to_swap[i];
to_swap[i] = to_swap[buff_len -1 -i];
to_swap[buff_len -1 -i] = swap_buff;
}
}
}