Linux:uint64_t主机字节序转网络字节序

/ Linux / 没有评论 / 205浏览

转换代码

#include <iostream>
#include <arpa/inet.h>

unsigned long long htonll(unsigned long long val)
{
    if (__BYTE_ORDER == __LITTLE_ENDIAN)
    {
        return (((unsigned long long)htonl((int)((val << 32) >> 32))) << 32) | (unsigned int)htonl((int)(val >> 32));
    }
    else if(__BYTE_ORDER == __BIG_ENDIAN)
    {
        return val;
    }
}

int main()
{
    uint64_t aaa = htonll(43);
    std::cout << aaa << std::endl;

    uint64_t bb = htonll(aaa);

    std::cout <<  bb <<  std::endl;

    return 0;
}