主机到网络双重?

发布于 2024-11-09 06:41:07 字数 623 浏览 0 评论 0原文

我想通过网络发送一些双精度浮点数。 (标准 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;
        }  
    }
}

I'd like to send some double precision floating point numbers over the network. (standard C, standard sockets) There is no htond or ntohd to convert the data to and from network byte order. What should I do? I have a couple solutions in my head but I'd like to know what the common practice is.

(I'd also like to know what is the common practice for sending 64bit ints, like gint64 values used by gstreamer)

edit:
This is one solution I thought of. I presume it works for any size integers, but is it correct for doubles?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风夜微凉 2024-11-16 06:41:07

将其转换为约定的字符串格式并发送。不确定这是否是常见做法或什至是体面的做法,但它对我有用(确实,我当时并不关心性能,因为我没有发送太多值)。

Convert it to an agreed string format and send that. Not sure if it's common practice or even decent, but it worked for me (it is true I did not care about performance at that point since I wasn't sending very many values).

千里故人稀 2024-11-16 06:41:07

安德烈的意思是,由于不同计算机体系结构之间的差异,二进制浮点数在网络上不可信。超出字节顺序(大/小端)的差异。因此,如果您的数据有可能由不同的计算机体系结构处理,那么转换为字符串或使用 XDR 等库之类的操作确实是必要的。

整数和字符的简单格式只需调整字节序即可通过,但浮点会变得更加复杂。

What Andre is saying is that binary floating point numbers are not trustworthy across networks because of differences between different computer architectures. Differences that go beyond byte order (big/little endian). Thus things like converting to strings or use of libraries such as XDR, is really necessary if there is any chance your data is going to be processed by different computer architectures.

The simple formats of integers and characters can slip through with just endian adjustments but floating point gets more complex.

梦旅人picnic 2024-11-16 06:41:07

您可能想查看 XDR ,它首先在 rfc1014。显然,您希望找到一个为您的平台实现 XDR 的库。

You might want to look at XDR which was first defined in rfc1014. Obviously, you want to find a library which implements XDR for your platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文