C ++ htonll和背部 [英] C++ htonll and back

查看:519
本文介绍了C ++ htonll和背部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下code这是能够转换的int64_t网络字节顺序。现在我需要相反的code,如果网络字节顺序转换回我的小endian机器。
在code是这样的。

I have found the following code which is capable of converting an int64_t to network byte order. Now I need the opposite code, such that network byte order is converted back to my little endian machine. The code is this.

int64_t decode(void* value){
    int64_t vv = *((int64_t*) value);
    int num = 42;
    if(*(char *)&num == 42) //test big/little endian
        return (((uint64)htonl(vv)) << 32) + htonl(vv >> 32);
    else 
        return vv;
}

非常感谢!

推荐答案

在code为你的 htonll

#define htonll(x) ((((uint64_t)htonl(x)) << 32) + htonl((x) >> 32))

翻转字节端对端。如果你申请了两次,它恢复的价值到其原始状态。因此,可以使用相同的功能 ntohll

这篇关于C ++ htonll和背部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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