是否有任何与QUOT;标准" htonl样的功能在C ++ 64位整数? [英] Is there any "standard" htonl-like function for 64 bits integers in C++?

查看:257
本文介绍了是否有任何与QUOT;标准" htonl样的功能在C ++ 64位整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的memcache的协议,该协议,在某些点上,使用64位整数的实现。这些数据必须存储在网络字节顺序。

我希望有一些 uint64_t中htonll(uint64_t中值)函数来完成的改变,但不幸的是,如果它存在,我怎么也找不到它。

所以,我有1个或2个问题:

  • 是否有任何的移动(在Windows,Linux,AIX)标准功能做到这一点?
  • 如果没有这样的功能,你会如何实现它?

我心里有一个基本的实现,但我不知道如何检查的字节顺序在编译时使code便携。所以,你的帮助是非常欢迎在这里;)

感谢你。


下面是最终的解决方案我写的,感谢布赖恩的解决方案。

  uint64_t中htonll(uint64_t中值)
{
    //答案是42
    静态const int的NUM = 42;

    //检查字节顺序
    如果(* reinter pret_cast<为const char *>(试验#)== NUM​​)
    {
        常量uint32_t的high_part = htonl(的static_cast< uint32_t的>(值GT;> 32));
        常量uint32_t的low_part = htonl(的static_cast< uint32_t的>(值放大器; 0xFFFFFFFFLL));

        回报(的static_cast< uint64_t中>(low_part)<< 32)| high_part;
    } 其他
    {
        返回值;
    }
}
 

解决方案

您可能正在寻找 bswap_64 我认为这是支持pretty的多随处可见,但我不会T称它为标准。

您可以轻松地创建带有值为1的int,铸造你的廉政局的地址作为的char *检查字节序和检查的第一个字节的值。

例如:

  INT NUM = 42;
如果(*(字符*)试验#== 42)
{
   //小端
}
其他
{
   //大端
}
 

知道了这一点,你也可以做一个简单的函数,它的交换。


您也可以随时使用升压其中包含端宏哪些是可移植的跨平台。

I'm working on an implementation of the memcache protocol which, at some points, uses 64 bits integer values. These values must be stored in "network byte order".

I wish there was some uint64_t htonll(uint64_t value) function to do the change, but unfortunately, if it exist, I couldn't find it.

So I have 1 or 2 questions:

  • Is there any portable (Windows, Linux, AIX) standard function to do this ?
  • If there is no such function, how would you implement it ?

I have in mind a basic implementation but I don't know how to check the endianness at compile-time to make the code portable. So your help is more than welcome here ;)

Thank you.


Here is the final solution I wrote, thanks to Brian's solution.

uint64_t htonll(uint64_t value)
{
    // The answer is 42
    static const int num = 42;

    // Check the endianness
    if (*reinterpret_cast<const char*>(&num) == num)
    {
        const uint32_t high_part = htonl(static_cast<uint32_t>(value >> 32));
        const uint32_t low_part = htonl(static_cast<uint32_t>(value & 0xFFFFFFFFLL));

        return (static_cast<uint64_t>(low_part) << 32) | high_part;
    } else
    {
        return value;
    }
}

解决方案

You are probably looking for bswap_64 I think it is supported pretty much everywhere but I wouldn't call it standard.

You can easily check the endianness by creating an int with a value of 1, casting your int's address as a char* and checking the value of the first byte.

For example:

int num = 42;
if(*(char *)&num == 42)
{
   //Little Endian
}
else
{
   //Big Endian
} 

Knowing this you could also make a simple function that does the swapping.


You could also always use boost which contains endian macros which are portable cross platform.

这篇关于是否有任何与QUOT;标准&QUOT; htonl样的功能在C ++ 64位整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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