8

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

 3 years ago
source link: https://www.lpime.cn/article/107
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Linux:uint64_t主机字节序转网络字节序 - Ryan_naiquan

字节序

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

本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2020/12/23 10:05


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK