• 小端存储转大端存储 & 大端存储转小端存储


     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

  • 相关阅读:
    C#的集合类(二)Hashtable、SortedList、Dictionary
    下载文件-JavaScript
    ASP.NET(C#)图片加文字、图片水印
    C# DataTable 互转 List<T>
    C# 将 DataTable 转 List<T>、首行转 T
    C# 金额转中文大写
    监听端口守护进程
    数据库操作的九大步骤
    日志方法
    sqlserver 查找所有子级
  • 原文地址:https://www.cnblogs.com/azbane/p/11303592.html
Copyright © 2020-2023  润新知