• Qt通用方法及类库7


    函数名

        //int转字节数组
        static QByteArray intToByte(int i);
        static QByteArray intToByteRec(int i);
    
        //字节数组转int
        static int byteToInt(const QByteArray &data);
        static int byteToIntRec(const QByteArray &data);
        static quint32 byteToUInt(const QByteArray &data);
        static quint32 byteToUIntRec(const QByteArray &data);
    
        //ushort转字节数组
        static QByteArray ushortToByte(ushort i);
        static QByteArray ushortToByteRec(ushort i);
    
        //字节数组转ushort
        static int byteToUShort(const QByteArray &data);
        static int byteToUShortRec(const QByteArray &data);
    

    函数体

    QByteArray QUIHelper::intToByte(int i)
    {
        QByteArray result;
        result.resize(4);
        result[3] = (uchar)(0x000000ff & i);
        result[2] = (uchar)((0x0000ff00 & i) >> 8);
        result[1] = (uchar)((0x00ff0000 & i) >> 16);
        result[0] = (uchar)((0xff000000 & i) >> 24);
        return result;
    }
    
    QByteArray QUIHelper::intToByteRec(int i)
    {
        QByteArray result;
        result.resize(4);
        result[0] = (uchar)(0x000000ff & i);
        result[1] = (uchar)((0x0000ff00 & i) >> 8);
        result[2] = (uchar)((0x00ff0000 & i) >> 16);
        result[3] = (uchar)((0xff000000 & i) >> 24);
        return result;
    }
    
    int QUIHelper::byteToInt(const QByteArray &data)
    {
        int i = data.at(3) & 0x000000ff;
        i |= ((data.at(2) << 8) & 0x0000ff00);
        i |= ((data.at(1) << 16) & 0x00ff0000);
        i |= ((data.at(0) << 24) & 0xff000000);
        return i;
    }
    
    int QUIHelper::byteToIntRec(const QByteArray &data)
    {
        int i = data.at(0) & 0x000000ff;
        i |= ((data.at(1) << 8) & 0x0000ff00);
        i |= ((data.at(2) << 16) & 0x00ff0000);
        i |= ((data.at(3) << 24) & 0xff000000);
        return i;
    }
    
    quint32 QUIHelper::byteToUInt(const QByteArray &data)
    {
        quint32 i = data.at(3) & 0x000000ff;
        i |= ((data.at(2) << 8) & 0x0000ff00);
        i |= ((data.at(1) << 16) & 0x00ff0000);
        i |= ((data.at(0) << 24) & 0xff000000);
        return i;
    }
    
    quint32 QUIHelper::byteToUIntRec(const QByteArray &data)
    {
        quint32 i = data.at(0) & 0x000000ff;
        i |= ((data.at(1) << 8) & 0x0000ff00);
        i |= ((data.at(2) << 16) & 0x00ff0000);
        i |= ((data.at(3) << 24) & 0xff000000);
        return i;
    }
    
    QByteArray QUIHelper::ushortToByte(ushort i)
    {
        QByteArray result;
        result.resize(2);
        result[1] = (uchar)(0x000000ff & i);
        result[0] = (uchar)((0x0000ff00 & i) >> 8);
        return result;
    }
    
    QByteArray QUIHelper::ushortToByteRec(ushort i)
    {
        QByteArray result;
        result.resize(2);
        result[0] = (uchar)(0x000000ff & i);
        result[1] = (uchar)((0x0000ff00 & i) >> 8);
        return result;
    }
    
    int QUIHelper::byteToUShort(const QByteArray &data)
    {
        int i = data.at(1) & 0x000000FF;
        i |= ((data.at(0) << 8) & 0x0000FF00);
    
        if (i >= 32768) {
            i = i - 65536;
        }
    
        return i;
    }
    
    int QUIHelper::byteToUShortRec(const QByteArray &data)
    {
        int i = data.at(0) & 0x000000FF;
        i |= ((data.at(1) << 8) & 0x0000FF00);
    
        if (i >= 32768) {
            i = i - 65536;
        }
    
        return i;
    }
    
  • 相关阅读:
    mvc Controller类介绍
    Mvc全局过滤器与Action排除
    MVC项目实践,在三层架构下实现SportsStore-03,Ninject控制器工厂等
    EF查询数据库框架的搭建
    MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等
    BootstrapTable与KnockoutJS相结合实现增删改查功能
    JSON详解
    Asp.net管道模型(管线模型)
    NET/ASP.NET MVC Controller 控制器(一:深入解析控制器运行原理)
    .NET/ASP.NET Routing路由(深入解析路由系统架构原理)
  • 原文地址:https://www.cnblogs.com/feiyangqingyun/p/12732185.html
Copyright © 2020-2023  润新知