zoukankan      html  css  js  c++  java
  • 小端存储转大端存储 & 大端存储转小端存储

     1、socket编程常用的相关函数:htons、htonl、ntohs、ntohl

      h:host   n:network      s:string    l:long

    2、基本数据类型,2字节,4字节,8字节的转换如下:

    trytry
    
    template <typename T>
    T transformBigToLittleEndian(const T &BiValue)
    {
        unsigned short sizeCount = sizeof(T);
        T liValue;
    
        if (sizeCount == 2)
        {
            liValue = ((BiValue & 0xFF00) >> 8)
                |((BiValue & 0x00FF) << 8);
        }
        else if (sizeCount == 4)
        {
            liValue = ((BiValue & 0xFF000000) >> 24)
                | ((BiValue & 0x00FF0000) >> 8)
                | ((BiValue & 0x0000FF00) << 8)
                | ((BiValue & 0x000000FF) << 24);
        }
        else if (sizeCount == 8)
        {
            liValue = ((BiValue & 0xFF00000000000000) >> 56)
                | ((BiValue & 0x00FF000000000000) >> 40)
                | ((BiValue & 0x0000FF0000000000) >> 24)
                | ((BiValue & 0x000000FF00000000) >> 8)
                | ((BiValue & 0x00000000FF000000) << 8)
                | ((BiValue & 0x0000000000FF0000) << 24)
                | ((BiValue & 0x000000000000FF00) << 40)
                | ((BiValue & 0x00000000000000FF) << 56);
        }
        return liValue;
    }
    template <typename T>
    T transformLittleToBigEndian(const T & liValue)
    {
        unsigned short sizeCount = sizeof(T);
        T BiValue;
    
        if (sizeCount == 2)
        {
            BiValue |= ((liValue & 0x00FF) << 8);
            BiValue |= ((liValue & 0xFF00) >> 8);
        }
        else if (sizeCount == 4)
        {
            BiValue = ((liValue & 0x000000FF) << 24)
                | ((liValue & 0x0000FF00) << 8)
                | ((liValue & 0x00FF0000) >> 8)
                | ((liValue & 0xFF000000) >> 24);
        }
        else if (sizeCount == 8)
        {
            BiValue |= ((liValue & 0x00000000000000FF) << 56);
            BiValue |= ((liValue & 0x000000000000FF00) << 40);
            BiValue |= ((liValue & 0x0000000000FF0000) << 24);
            BiValue |= ((liValue & 0x00000000FF000000) << 8);
            BiValue |= ((liValue & 0x000000FF00000000) >> 8);
            BiValue |= ((liValue & 0x0000FF0000000000) >> 24);
            BiValue |= ((liValue & 0x00FF000000000000) >> 40);
            BiValue |= ((liValue & 0xFF00000000000000) >> 56);
        }
        return BiValue;
    }

    int main()
    {
    transformBigToLittleEndian();
    int value = 1;
    value = transformLittleToBigEndian(value);
    cout << "int value = 1 从小端转为大端后 biA = " << value << endl;

    
    

    value = transformBigToLittleEndian(value);
    cout << "再从大端转换为小端后的值 = " << value << endl;
    }

    如何判断自己计算机的存储方式是大端存储还是小端存储:https://www.cnblogs.com/azbane/p/11303463.html

  • 相关阅读:
    WINCE串口打印信息的屏蔽与打开
    WINCE系统时间设置
    WINCE调试常见错误
    今天开始研究arcgisserver
    今天开始,放弃项目管理,做技术了
    java数据库连接
    [转]建一个XMLHttpRequest对象池
    Linux下jsp开发环境,apache,tomcat服务器配置过程
    div+css布局漫谈
    新图纸总结
  • 原文地址:https://www.cnblogs.com/azbane/p/11303592.html
Copyright © 2011-2022 走看看