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

  • 相关阅读:
    JS 加载html 在IE7 IE8下 可调试
    css 实现三角形 实现过程
    JSONP 理解 和 实例 讲解
    js 闭包 理解
    常见input输入框 点击 发光白色外阴影 focus
    常用垂直居中 随窗口改变一直居中布局 实例
    修改wampserver 默认localhost 和phpmyadmin 打开链接
    validate jquery 注册页面使用实例 详解
    mysql 查询表结构 查询索引
    wampserver 绑定域名 外部可以正常访问
  • 原文地址:https://www.cnblogs.com/azbane/p/11303592.html
Copyright © 2011-2022 走看看