如何管理从网络字节序双 [英] How to manage endianess of double from network

查看:131
本文介绍了如何管理从网络字节序双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然而,这个问题是多了还是少了什么我搜索:
 我收到了双来自网络,我想带codeD得当在我的机器。

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 这code:


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);

但在情况下,我收到一个双击,我不知道该怎么办。

该问题的答案建议做:

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

不过,如果我引用<一个href=\"http://forums.$c$cguru.com/showthread.php?298741-C-General-What-do-ntohl%28%29-and-htonl%28%29-actually-do\"相对=nofollow>本网站:

的ntohl()函数将无符号整数,从网络字节顺序到主机字节顺序netlong。
当两个字节顺序是不同的,这意味着该数据的字节序将被改变。当两个字节顺序是相同的,该数据将不被改变。

相反@ GManNickG的问题的答案的总是做反转的std ::逆转

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

我错了考虑这个答案是假的? (以字节序的网络管理在多大程度上使用 ntohl 的建议,虽然它不是precisely在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).

在结束:我应该分割我的双击成4个字节两部分,在应用 ntohl 功能两部分?是否有更多的cannonical解决方案?

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));
}

这篇关于如何管理从网络字节序双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆