如何管理来自网络的双重字节序

2022-01-19 00:00:00 c network-programming c++ endianness libpq

我对这个问题的答案有一个大问题 C++ 中的交换位对于双

I have a BIG problem with the answer to this question Swap bits in c++ for a double

然而,这个问题或多或少是我要寻找的:我从网络收到了一个 double,我想在我的机器中正确编码它.

Yet, this question is more or less what I search for: I receive a double from the network and I want to encoded it properly in my machine.


在我收到 int 的情况下,我使用 ntohl 执行此代码:


In the case I receive an int I perform this code using ntohl :

int * piData = reinterpret_cast<int*>((void*)pData);

//manage endianness of incomming network data 
unsigned long ulValue = ntohl(*piData);
int iValue = static_cast<int>(ulValue);

但如果我收到 double,我不知道该怎么办.

But in the case I receive an double, I don't know what to do.

问题的答案建议这样做:

The answer to the question suggest to do:

template <typename T>
void swap_endian(T& pX)
{
    char& raw = reinterpret_cast<char&>(pX);
    std::reverse(&raw, &raw + sizeof(T));
}

但是,如果我引用 这个网站:

However , if I quote this site:

ntohl() 函数将无符号整数 netlong 从网络字节顺序转换为主机字节顺序.当两个字节顺序不同时,这意味着数据的字节序会改变.当两个字节顺序相同时,数据不会改变.

相反,@GManNickG 对问题的回答 always does the inversion with std::reverse .

On the contrary @GManNickG's answer to the question always does the inversion with std::reverse .

考虑到这个答案是错误的,我错了吗?(在使用 ntohl 建议的字节序网络管理范围内,尽管在 OP 问题的标题中没有准确说明).

Am I wrong considering that this answer is false ? ( in the extent of network management of endianess which the use of ntohl suggest though it was not precisely said in the title of the OP question).

最后:我应该将我的 double 分成两部分,每部分 4 个字节,并在这两部分上应用 ntohl 函数吗?有没有更规范的解决方案?

In the end: Should I split my double into two parts of 4 bytes and apply the ntohl function on the two parts ? Are there more cannonical solutions ?

C 中也有这个有趣的问题,主机到网络双重?,但它限制到 32 位值.答案说由于架构差异,双打应该转换为字符串......我还将使用音频样本,我是否真的应该考虑将所有样本转换为我的数据库中的字符串?(双打来自我通过网络查询的数据库)

There's also this interesting question in C, host to network double?, but it limits to 32 bits values. And the answer says doubles should be converted to strings because of architecture differences... I'm also gonna work with audio samples, should I really consider converting all the samples to strings in my database ? ( the doubles come from a database that I query over the network)

推荐答案

如果你的双打是 IEEE 754 格式,那你应该还可以.现在你必须将它们的 64 位分成两个 32 位的一半,然后以大端顺序(即网络顺序)传输它们;

If your doubles are in IEEE 754 format that you should be relatively OK. Now you have to divide their 64 bits into two 32-bit halves and then transmit them in big-endian order (which is network order);

怎么样:

void send_double(double d) {
    long int i64 = *((reinterpret_cast<int *>)(&d)); /* Ugly, but works */
    int hiword = htonl(static_cast<int>(i64 >> 32));
    send(hiword);
    int loword = htonl(static_cast<int>(i64));
    send(loword);
}

double recv_double() {
    int hiword = ntohl(recv_int());
    int loword = ntohl(recv_int());
    long int i64 = (((static_cast<long int>) hiword) << 32) | loword;
    return *((reinterpret_cast<double *>(&i64));
}

相关文章