QT 常用的数据转换

/**
* @file         DataTpye_Qt.h
* @brief        This is a common data transformation of qt for portable use.
* @details  This is the detail description.
* @author       Huang Zhudong
* @date     2019-05-31
* @version  v1.4
* @par Copyright (c):
* @par History: v1.1 增加HexToBin函数
*               v1.2 增加QByteArrayToUint32()/Uint32ToQByteArray()/UintToQString()函数,同时修改HexToBin函数支持64KB以上的转换。
*				v1.3 增加QByteArray ChangeEndian(QByteArray Data)/QByteArray OutToBin(QString OutFile)函数,支持Out文件的转换。
*               v1.4 修复QByteArray OutToBin(QString OutFile)函数中地址不连续的问题。
*               v1.5 修复HexToBin函数中04线性地址偏移的错误。
*/
#ifndef DATATYPE_QT_H
#define DATATYPE_QT_H
#include "QString"
#include "QStringList"
#include "QFile"
#include "QProcess"
#include "stdint.h"
#include "QList"
extern QList FragmentEndAddr;
extern char FillChar;  // 填充位

struct BinFormat{
    QByteArray BinData;
    uint32_t BStartAddr;
    uint32_t BEndAddr;
}; //将返回的bin文件带起始地址

// 分割数据的起始地址和结束地址
struct SplitInfo{
    QString startIndex;
    QString EndIndex;
    QString offsetIndex;
};

class DataType_Qt
{
public:
    DataType_Qt();
};


char ConvertHexChar(char ch);
QByteArray QString2Hex(QString str);
QByteArray QByteArray2Hex(QByteArray str);
QByteArray QStringToQByteArray(QString qstr);
QByteArray QStringToHex(QString str);
QByteArray QStringToChar(QString str);
uint32_t QStringToUint32(QString arry);
uint32_t QStringToDec(QString arry);
uint32_t QByteArrayToUint32_Num(QByteArray arry);
uint32_t QByteArrayToUint32(QByteArray arry);
QByteArray Uint32ToQByteArray(uint32_t data);
QString UintToQString(size_t data);
QByteArray ChangeEndian(QByteArray Data);
//Hex转换Bin文件
//pBin: 存放数据的缓冲
//返回值: *.Bin文件的内容
BinFormat HexToBin(QByteArray Hex, QString Byte8Or16);
BinFormat OutToBin(QString OutFile); //Out转Bin
QByteArray BinToHex(BinFormat bin);
QByteArray HexCRC(QByteArray Data);
BinFormat BinFix(BinFormat bin, uint32_t DstStart, uint32_t DstEnd, QString Byte8Or16);
#endif // DATATYPE_QT_H
/**
* @file         DataTpye_Qt.cpp
* @brief        This is a common data transformation of qt for portable use.
* @details      This is the detail description.
* @author       Huang Zhudong
* @date         2019-08-20
* @version      v1.4
* @par Copyright (c):
* @par History:
*/

#include "DataType_Qt.h"
#include 
#include "qDebug"

char FillChar = '0'; // 烧录文件填充位,默认为'F'

DataType_Qt::DataType_Qt()
{

}




char ConvertHexChar(char ch)
{
    if((ch >= '0') && (ch <= '9')) return (ch - 0x30);
    else if((ch >= 'A') && (ch <= 'F')) return (ch - 'A' + 0x0a);
    else if((ch >= 'a') && (ch <= 'f')) return (ch - 'a' + 0x0a);
    else return (-1);
}

/**
* This function is used to convert two QString characters to Hex bases.
* @param[in]   QString
* @param[out]  QByteArray
* @par  * For example, if QString input = "3132333435";than QByteArrat output = QString2Hex(input);
*       the result is 0x12345.
* @par Created in 2018-11-12
*/
QByteArray QString2Hex(QString str)
{
    QByteArray senddata;
    int hexdata,lowhexdata;
    int hexdatalen = 0;
    int len = str.length();
    senddata.resize(len/2);
    unsigned char lstr,hstr;
    for(int i=0; i= len)
        break;
        lstr = str[i].toLatin1();
        hexdata = ConvertHexChar(hstr);
        lowhexdata = ConvertHexChar(lstr);
        if((hexdata == 16) || (lowhexdata == 16))
        break;
        else
        hexdata = hexdata * 16 + lowhexdata;
        i++;
        senddata[hexdatalen] = (char)hexdata;
        hexdatalen++;
    }
    senddata.resize(hexdatalen);
    return senddata;
}

/**
* This function is used to convert QString to QByteArray.
* @param[in]   QString "31"
* @param[out]  QByteArray '3' '1'
* @retval
* @par Created in 2018-11-12
*/
QByteArray QStringToQByteArray(QString qstr)
{
    QByteArray qbyte = qstr.toLatin1();
    return qbyte;
}

/**
* This function is used to convert QString to Hex.
* @param[in]   QString "31"
* @param[out]  QByteArray '3' '3' '3' '1'
* @retval
* @par Created in 2018-11-12
*/
QByteArray QStringToHex(QString str)
{
    QByteArray tmp1 = str.toLatin1();
    QByteArray tmp2 = tmp1.toHex();
    return tmp2;
}

/**
* This function is used to convert QString to Hex.
* @param[in]   QString '12345678'
* @param[out]  uint32_t 0x12345678
* @retval
* @par Created in 2019-07-19
*/
uint32_t QStringToUint32(QString arry)
{
    uint32_t res = 0;
    QByteArray bytes = arry.toUtf8();
    for(int i = 0; i < bytes.size(); i ++){
        char m = bytes.at(i);
        char tmp = ConvertHexChar(m);
        if(tmp == -1){ tmp = 0;}
        res = res << 4;
        res += tmp;
    }
    return res;
}
/**
* This function is used to convert QString to Hex.
* @param[in]   QString '12345678'
* @param[out]  12345678
* @retval
* @par Created in 2020-04-30
*/
uint32_t QStringToDec(QString arry)
{
    uint32_t res = 0;
    QByteArray bytes = arry.toUtf8();
    for(int i = 0; i < bytes.size(); i ++){
        char m = bytes.at(i);
        char tmp = ConvertHexChar(m);
        if(tmp == -1){ tmp = 0;}
        res = res *10;
        res += tmp;
    }
    return res;

}

/**
* This function is used to convert QByteArray to Hex.
* @param[in]   QByteArray arry = {'1', '2', '3'}
* @param[out]  uint32_t 0x123
* @retval
* @par Created in 2019-08-25
*/
uint32_t QByteArrayToUint32_Num(QByteArray arry)
{
    uint32_t res = 0;
    for(int i = 0; i < arry.size(); i ++){
        char m = arry.at(i);
        uint8_t tmp = ConvertHexChar(m);
        res = res << 4;
        res += tmp;
    }
    return res;
}

/**
* This function is used to convert QString to Hex.
* @param[in]   QByteArray '33' '32' '31' '30'
* @param[out]  uint32_t 0x33323130
* @retval
* @par Created in 2019-01-23
*/
uint32_t QByteArrayToUint32(QByteArray arry)
{
    uint32_t res = 0;
    for(int i = 0; i < arry.size(); i ++){
        uint8_t tmp = arry.at(i);
        res = res << 8;
        res += tmp;
    }
    return res;
}

/**
* This function is used to convert QString to Hex.
* @param[in]   uint32_t 0x33323130
* @param[out]  ByteArray '33' '32' '31' '30'
* @retval
* @par Created in 2019-01-23
*/
QByteArray Uint32ToQByteArray(uint32_t data){
    QByteArray res;
    for(int i = 0; i < 4; i++){
        uint8_t tmp = data & 0xFF;
        res.push_front(tmp);
        data = data >> 8;
    }
    return res;
}


/**
* This function is used to convert Uint to QString.
* @param[in]   uint32_t 0x3210
* @param[out] (Decimal) QString "12816" (Hexadecimal) QString "3210"
* @retval
* @par Created in 2019-01-24
*/
QString UintToQString(size_t data){
    QString res = QString("%1").arg(data,4,10,(QChar)'0').toUpper();// Decimal numeral output
//    QString res = QString("%1").arg(data,4,16,(QChar)'0').toUpper();// Hexadecimal numeral output
    return res;
}




/**
* This function is used to tranform .hex to .bin, and support more than 64KB.
* @param[in]   QByteArray pHex :020000040008F2\r\n
*              QString Byte8Or16: ST Intel Hex format; TI Ti Hex format
*              uint32_t Start: if HexStartAddr < Start, between with them filled with
* @param[out]  QByteArray pBin 0400
* @retval return NULL means that .hex fiel is broken.
* the struct of hex file data:
* | StartChar | Info_Len |  Info_Addr  | Info_Type | Info_Data | Info_CRC | EndChar |
* |    ':'    |  XX XX   | XX XX XX XX |   XX XX   |  XX … XX  |   XX XX  |   \r\n  |
* |   1Byte   |  2Byte   |    4Byte    |   2Byte   |   NByte   |   2Byte  |  2Byte  |
* @par Created in 2019-01-16
*/
QList FragmentEndAddr;
BinFormat HexToBin(QByteArray Hex, QString Byte8Or16)
{
    FragmentEndAddr.clear();  //清除分段结束地址
    //起始位 结束位 数据类型 定义
    QByteArray StartChar = ":";
    QByteArray EndChar = "\r\n"; //没有找到
    int EndCharSize = 2;
    QString Info_Type_Data = "00";
    QString Info_Type_EndFile = "01";
    QString Info_Type_ExtSegAddr = "02";
    QString Info_Type_StartSegAddr = "03";
    QString Info_Type_ExtLineAddr = "04";
    QString Info_Type_StartLineAddr = "05";
    bool GetStartAddr = false;
    BinFormat pBin;
    QStringList Info_Datas;
    int nEnd = Hex.lastIndexOf(":00000001");
    QByteArray pHex = Hex.mid(0,nEnd); // 避免最后一行没有回车
    if(nEnd <= 0){
        pBin.BinData = NULL;
        pBin.BStartAddr = 0;
        pBin.BEndAddr = 0;
    }
    else{
        uint32_t Info_AbsAddr_Last = 0x0000;
        uint32_t Info_AbsAddr = 0x0000;
        uint32_t Info_OffsetAddr = 0x0000;
        uint32_t fillCount = 0x0000;
        while(pHex.size()){
            QCoreApplication::processEvents();  // 不堵塞其他消息
            int StartIndex = pHex.indexOf(StartChar);
            int EndIndex = pHex.indexOf(EndChar);   EndCharSize = 2;
            if(EndIndex == -1) {EndIndex =  pHex.indexOf(EndChar[1]);EndCharSize = 1;}  // 如果没有找到\r\n,再找\n
            if((EndIndex == -1) || (StartIndex == -1)) {  // 没有找到开始或者结尾符
                pBin.BinData = NULL;
                pBin.BStartAddr = 0;
                pBin.BEndAddr = 0;
                return pBin;
            }
            QString Info = pHex.mid(StartIndex, EndIndex - StartIndex);
            QString Info_Len = Info.mid(1,2);
            QString Info_Addr = Info.mid(3,4);
            QString Info_Type = Info.mid(7,2);
            QString Info_CRC = Info.mid(EndIndex - 2, 2);
            QString Info_Data = Info.mid(StartIndex + 9, Info.size() - StartChar.size() - Info_Len.size() - Info_Addr.size() - Info_Type.size() - Info_CRC.size());
            QByteArray Date_len_tmp = QString2Hex(Info_Len);
            int Data_len = int(Date_len_tmp[0]);
            if(Data_len == Info_Data.size()/2){
                //如果是数据类型
                if(Info_Type == Info_Type_Data){
                    QByteArray Info_Addr_B = QString2Hex(Info_Addr);
                    Info_AbsAddr = Info_OffsetAddr + QByteArrayToUint32(Info_Addr_B);
                    // 获取起始地址
                    if(GetStartAddr == false) {pBin.BStartAddr = Info_AbsAddr & 0x00000000FFFFFFFF;GetStartAddr = true;} //保证32位数据
                    fillCount = Info_AbsAddr -(Info_AbsAddr_Last);
                    if(fillCount > 0){
                        QString data_tmp;
                        //判断是否为第一次有数值
                        if(!Info_Datas.isEmpty()){
                            if(Byte8Or16 == "ST"){
                                data_tmp.fill(FillChar, 2*fillCount); // ST的8位
                            }
                            else if(Byte8Or16 == "TI"){
                                data_tmp.fill(FillChar, 4*fillCount); //Ti的16位
                            }
                            Info_Datas.append(data_tmp);
//                            if(Info_AbsAddr >= Start){ //小于目标地址,跳出
//                                Info_Datas.append(data_tmp);
//                            }
                        }
//                        else{
//                            pBin.BStartAddr = Info_AbsAddr;
//                        }
                    }
                    Info_Datas.append(Info_Data);
//                    if(Info_AbsAddr >= Start){ //小于目标地址,跳出
//                        Info_Datas.append(Info_Data);
//                    }
                    if(Byte8Or16 == "ST"){
                        Info_AbsAddr +=  Data_len; // ST的8位,Ti的16位
                    }
                    else if(Byte8Or16 == "TI"){
                        Info_AbsAddr += Data_len/2;
                    }
                    Info_AbsAddr_Last =  Info_AbsAddr;
                }
                //扩展段地址记录
                else if(Info_Type == Info_Type_ExtSegAddr){
                    if(Info_Addr == "0000"){
                        QByteArray Info_Addr_B = QString2Hex(Info_Data);
                        Info_OffsetAddr = QByteArrayToUint32(Info_Addr_B); // 段扩展
                        Info_AbsAddr = Info_AbsAddr_Last + Info_OffsetAddr;
                    }
                }
                //扩展线性地址记录
                else if(Info_Type == Info_Type_ExtLineAddr){
                    if(Info_Addr == "0000"){
                        FragmentEndAddr << Info_AbsAddr; // 保存分段数据结束地址
                        QByteArray Info_Addr_B = QString2Hex(Info_Data);
                        Info_OffsetAddr = QByteArrayToUint32(Info_Addr_B) << 16;  // 线性偏移
                        Info_AbsAddr =  Info_OffsetAddr;
                    }

                }
            }
             pHex.remove(StartIndex, EndIndex - StartIndex + EndCharSize);

        }
//        if(Byte8Or16 == "ST"){
//           pBin.BEndAddr = Info_AbsAddr - 1; // 获取结束地址 = 下一次的绝对地址 - 1
//        }
//        else if(Byte8Or16 == "TI"){
//           pBin.BEndAddr = 2*(Info_AbsAddr - 1); // 获取结束地址 = 2*(下一次的绝对地址 - 1)
//        }
         pBin.BEndAddr = Info_AbsAddr - 1; // 获取结束地址 = 下一次的绝对地址 - 1
        while(!Info_Datas.isEmpty()){
            QCoreApplication::processEvents();  // 不堵塞其他消息
            pBin.BinData += QString2Hex(Info_Datas.at(0));
            Info_Datas.removeFirst();
        }        
    }
    qDebug() << "pBin.StartAdd: 0x" << QString::number(pBin.BStartAddr,16).toUpper()
             << "pBin.EndAdd: 0x" << QString::number(pBin.BEndAddr,16).toUpper();
    return pBin;
}

/**
* This function is used to tranform .hex to .bin, and support more than 64KB.
* @param[in]   .out file Path
* @param[out]  QByteArray
* @retval return NULL means that .out fiel is broken.
* @par Created in 2019-05-16
*/
BinFormat OutToBin(QString OutFile)
{
    QString EXEName_src = ":/EXEbin/hex2000.exe";
    QString EXEName_Dst = "~hex2000.exe";
    QFile EXEFile_src(EXEName_src);
    QFile EXEFile_Dst(EXEName_Dst);
    //如果exe目标文件存在
    while(EXEFile_Dst.exists()){
        EXEName_Dst.insert(0,"1");
        EXEFile_Dst.setFileName(EXEName_Dst);
    }
   //对资源里的exe进行重新生成
    if(EXEFile_Dst.open (QIODevice::WriteOnly)){
       EXEFile_src.open(QIODevice::ReadOnly);
       QByteArray tmp = EXEFile_src.readAll();
       EXEFile_Dst.write(tmp);
    }
    EXEFile_Dst.close();

    //生成hex临时文件
    QString HexFilePath = "~tmp.hex";
    QFile HexFile(HexFilePath);
    //如果hex目标文件存在
    while(HexFile.exists()){
        HexFilePath.insert(0,"1");
        HexFile.setFileName(HexFilePath);
    }
    QString CMD = EXEName_Dst + " " + "\"" + OutFile + "\"" + " -order MS -romwidth 16 -i -o " + HexFilePath;
    qDebug() << CMD ;
//    QProcess::startDetached(CMD);
    //执行CMD命令
    char* ptr;
    QByteArray ba = CMD.toLocal8Bit(); //支持含中文
    ptr = ba.data();
    system(ptr);
    //读取Hex文件内容
    BinFormat FileArry_Bin;
    if(HexFile.open(QIODevice::ReadOnly)){
        QByteArray FileArry_Hex = HexFile.readAll(); //读取hex数据
        BinFormat FileArry_Bin_tmp = HexToBin(FileArry_Hex, "TI"); //hexTobin
        FileArry_Bin.BinData = ChangeEndian(FileArry_Bin_tmp.BinData); //小端模式
//        FileArry_Bin.BinData = FileArry_Bin_tmp.BinData;  // 大端模式
        FileArry_Bin.BStartAddr = FileArry_Bin_tmp.BStartAddr;
        FileArry_Bin.BEndAddr = FileArry_Bin_tmp.BEndAddr;
    }
    else{
        FileArry_Bin.BinData = NULL;
        FileArry_Bin.BStartAddr = 0;
        FileArry_Bin.BEndAddr = 0;
    }

    EXEFile_Dst.remove(); //删掉exe文件
    HexFile.remove(); //删掉hex文件

    return FileArry_Bin;
}
/**
* This function is used to tranform little endian to .bin, and support more than 64KB.
* @param[in]   QByteArray 02 00
* @param[out]  QByteArray 00 02
* @retval
* @par Created in 2019-05-17
*/
QByteArray ChangeEndian(QByteArray Data)
{
    int DataSize= Data.size();
    QByteArray DataOut;
    //如果是偶数
    if(DataSize %2 == 0){
        for(int i = 0; i < DataSize; i++){
            DataOut[i] = Data[i+1];
            DataOut[i+1] = Data[i];
            i += 1;
        }
    }

    //如果是奇数
    if(DataSize %2 == 1){
        DataSize -= 1;
        for(int i = 0; i < DataSize; i++){
            DataOut[i] = Data[i+1];
            DataOut[i+1] = Data[i];
            i += 1;
        }
        DataOut[DataSize] = Data[DataSize];
    }

    return DataOut;
}
#define Type_Data              "00"
#define Type_EndFile           "01"
#define Type_ExtSegAddr        "02"
#define Type_StartSegAddr      "03"
#define Type_ExtLineAddr       "04"
#define Type_StartLineAddr     "05"
#define ToWriteMaxSize          0x20
QByteArray BinToHex(BinFormat bin)
{
    QByteArray begin_ToWrite = ":";
    QByteArray Addr_ToWrite;         // 单句地址
    QByteArray Num_ToWrite;         // 单句写多长
    QByteArray Type_ToWrite;
    QByteArray CRC_ToWrite;         // 单句CRC
    QByteArray Data_ToWrite;        // 单句数据
    QByteArray End_ToWrite = "\r\n";
    QByteArrayList HexDataList;

    int Type_ExtLineAddrNum = bin.BStartAddr/0x10000;  //线地址偏移
    int Num_ToWrite_s = 0;
    if(Type_ExtLineAddrNum != 0){  // 开始线地址偏移信息":02 0000 04 0002 F8"
        Num_ToWrite = "02";
        Addr_ToWrite = "0000";
        Type_ToWrite = Type_ExtLineAddr;
        Data_ToWrite = QByteArray::number(Type_ExtLineAddrNum,16).toUpper();
        while(Data_ToWrite.size() < (2*QByteArrayToUint32_Num(Num_ToWrite))){ Data_ToWrite.insert(0,"0");} // 前面补全零
        QByteArray HexData = begin_ToWrite + Num_ToWrite + Addr_ToWrite + Type_ToWrite + Data_ToWrite;
        CRC_ToWrite = HexCRC(HexData);
        HexData += CRC_ToWrite + End_ToWrite;
        HexDataList.append(HexData);
    }
    int NowAddress = bin.BStartAddr%(0x10000);  //当前地址
    int Num_rest = 0x10000 - NowAddress;   // 剩下的地址长度
    bool ExtLineWrite = false;
    while(bin.BinData.size() != 0){
        QCoreApplication::processEvents();  // 不堵塞其他消息
        if((Num_rest >= ToWriteMaxSize) && (bin.BinData.size() >= ToWriteMaxSize)){ // 如果可写数据和剩下容量都大于20,则只写20个
            Num_ToWrite = QByteArray::number(ToWriteMaxSize, 16).toUpper();
            ExtLineWrite = false;
        }
        else if((Num_rest >= ToWriteMaxSize) && (bin.BinData.size() < ToWriteMaxSize)){ // 如果可写数据小于20,剩下容量都大于20,则只写少于20个
            Num_ToWrite = QByteArray::number(bin.BinData.size(), 16).toUpper();
            ExtLineWrite = false;
        }
        else if((Num_rest < ToWriteMaxSize) && (bin.BinData.size() >= ToWriteMaxSize)){ // 如果可写数据大于等于20,剩下容量小于20,则只少于20个,并分段
            Num_ToWrite =  QByteArray::number(Num_rest, 16).toUpper();
            ExtLineWrite = true;
        }
        else if((Num_rest < ToWriteMaxSize) && (bin.BinData.size() < ToWriteMaxSize)){ // 如果可写数据和剩下容量都小于20,则需要进一步判断
            if(Num_rest >= bin.BinData.size()){ // 如果剩下容量大于等于可写数据
                Num_ToWrite = QByteArray::number(bin.BinData.size(), 16).toUpper();
                ExtLineWrite = false;
            }
            else{ // 如果剩下容量小于可写数据
                Num_ToWrite = QByteArray::number(Num_rest, 16).toUpper();
                ExtLineWrite = true;
            }
        }
        if(QByteArrayToUint32_Num(Num_ToWrite) != 0){
            while(Num_ToWrite.size() < 2){ Num_ToWrite.insert(0,"0");} // 前面补全零
            Addr_ToWrite = QByteArray::number(NowAddress, 16).toUpper();
            while(Addr_ToWrite.size() < 4){ Addr_ToWrite.insert(0,"0");} // 前面补全零
            Type_ToWrite = Type_Data;
            Data_ToWrite = bin.BinData.mid(0, QByteArrayToUint32_Num(Num_ToWrite));

            QByteArray HexData = begin_ToWrite + Num_ToWrite + Addr_ToWrite + Type_ToWrite + QByteArray2Hex(Data_ToWrite);
            CRC_ToWrite = HexCRC(HexData);
            HexData += CRC_ToWrite  + End_ToWrite;
            HexDataList.append(HexData); // 保存数据

            NowAddress += QStringToUint32(Num_ToWrite);
            bin.BinData.remove(0, QStringToUint32(Num_ToWrite));
            Num_rest -= QStringToUint32(Num_ToWrite);
            Num_ToWrite = "00";  // 关闭
        }
        if(ExtLineWrite){
            Num_ToWrite = "02";
            Addr_ToWrite = "0000";
            Type_ToWrite = Type_ExtLineAddr;
            Data_ToWrite = QByteArray::number(++Type_ExtLineAddrNum,16).toUpper();
            while(Data_ToWrite.size() < (2*QByteArrayToUint32_Num(Num_ToWrite))){ Data_ToWrite.insert(0,"0");} // 前面补全零
            QByteArray HexData = begin_ToWrite + Num_ToWrite + Addr_ToWrite + Type_ToWrite + Data_ToWrite;
            CRC_ToWrite = HexCRC(HexData);
            HexData += CRC_ToWrite + End_ToWrite;
            HexDataList.append(HexData);

            NowAddress = 0x0000;
            Num_rest = 0x10000 - NowAddress;
            ExtLineWrite = false; // 关闭
        }
    }
    if(bin.BinData.size() == 0){ //输入最后结束字符段 “:00 0000 01 FF”
        Num_ToWrite = "00";
        Addr_ToWrite = "0000";
        Type_ToWrite = Type_EndFile;
        Data_ToWrite = "";
        while(Data_ToWrite.size() < (2*QByteArrayToUint32_Num(Num_ToWrite))) { Data_ToWrite = "0" + Data_ToWrite;} // 前面补全零
        QByteArray HexData = begin_ToWrite + Num_ToWrite + Addr_ToWrite + Type_ToWrite + Data_ToWrite;
        CRC_ToWrite = HexCRC(HexData);
        HexData += CRC_ToWrite  + End_ToWrite;
        HexDataList.append(HexData);
    }
    QByteArray tmp;
    for(int i = 0; i < HexDataList.size(); i++){
        tmp += HexDataList.at(i);
    }
    return QByteArray(tmp);
}
/**
* This function is used to get CRC in hex file.
* @param[in]   QByteArray Data = {':','0','2','0','0','0','0','0','4','0','0','0','1'}
* @param[out]  QByteArray CRC  = {'F','9'}
* @retval
* @par Created in 2019-08-23
*/
QByteArray HexCRC(QByteArray Data)
{
    Data.remove(0,1); // 删除“:”
    uint8_t CRC = 0x100;
    while(Data.size() != 0){
        uint8_t tmp = QByteArrayToUint32_Num(Data.mid(0,2));
        CRC -= tmp;
        Data.remove(0,2);
    }
    CRC &= 0x000000ff;
    QByteArray ret = QByteArray::number(CRC, 16).toUpper();
    while(ret.size() < 2) {
        ret.insert(0,"0");
    }
    return ret; // 转为16位的数据大写

}

/**
* This function is used to fill with "FF" to bin file.
* @param[in]   BinFormat bin: {
*                               {00,01,02,03};
*                               0;
*                               3;
*                              }
*              int DstStart: 1
*              int DstEnd: 2
* @param[out]  BinFormat output: {
*                                {01,02};
*                                1;
*                                2;
*                               }
* @retval
* @par Created in 2019-08-23
*/
BinFormat BinFix(BinFormat bin, uint32_t DstStart, uint32_t DstEnd, QString Byte8Or16)
{
    int DstStart_Store = DstStart;
    BinFormat ret;
    if(Byte8Or16 == "TI"){
        int Distance = DstEnd - DstStart; // 两者之间的个数
        DstStart = DstStart - (bin.BStartAddr - DstStart);
        DstEnd = DstStart + 2*Distance + 1;  // 偏移地址 + 起始地址
        bin.BEndAddr = 2*bin.BEndAddr - bin.BStartAddr + 1;
        qDebug() << "DstStart:" << QString::number(DstStart,16).toUpper();
        qDebug() << "DstEnd:" << QString::number(DstEnd,16).toUpper();
    }
    ret.BStartAddr = DstStart;
    ret.BEndAddr = DstEnd;
    ret.BinData.resize(DstEnd - DstStart + 1);
    char FillC = ConvertHexChar(FillChar) & 0x0F;
    FillC = ((FillC << 4) & 0xF0) + FillC;
    ret.BinData.fill(FillC, DstEnd - DstStart + 1);
    qDebug() << "ret.size(1)" << ret.BinData.size();
    if(DstStart < bin.BStartAddr){
        if(DstEnd < bin.BStartAddr){  //目标起始和结尾都在bin文件头之前
            int FillCount = DstEnd - DstStart + 1;
            QByteArray tmp;
            tmp.fill(FillC, FillCount);
            ret.BinData.replace(0, FillCount, tmp);
        }
        else if((DstEnd >= bin.BStartAddr) && (DstEnd <= bin.BEndAddr)){ //目标起始在bin文件头之前,结尾在bin文件内部
            int FillCount = DstEnd - bin.BStartAddr + 1;
            ret.BinData.replace(bin.BStartAddr - DstStart, FillCount, bin.BinData.mid(0, FillCount));
        }
        else if(DstEnd > bin.BEndAddr){ //目标起始在bin文件头之前,结尾在bin文件外部
            int FillCount = bin.BEndAddr - bin.BStartAddr + 1;
            ret.BinData.replace(bin.BStartAddr - DstStart, FillCount, bin.BinData.mid(0, FillCount));
            qDebug() << "bin.BinData.mid(0, FillCount).size" << bin.BinData.size();
            qDebug() << "FillCount" << FillCount;
        }
        else{ //其他
            ret.BinData.clear();
        }
    }

    else if( (DstStart >= bin.BStartAddr) && (DstStart <= bin.BEndAddr)){
        if((DstEnd >= bin.BStartAddr) && (DstEnd <= bin.BEndAddr)){ //目标起始和结尾都在bin文件之内
            int FillCount = DstEnd - DstStart + 1;
            ret.BinData.replace(0, FillCount, bin.BinData.mid(DstStart - bin.BStartAddr, FillCount));
        }
        else if(DstEnd > bin.BEndAddr){ //目标起始在bin文件之内,结尾在bin文件之外
            int FillCount = bin.BEndAddr - DstStart + 1;
            ret.BinData.replace(0, FillCount, bin.BinData.mid(DstStart - bin.BStartAddr, FillCount));
        }
        else{ //其他
            ret.BinData.clear();
        }
    }

    else if (DstStart > bin.BEndAddr){
        if(DstEnd > bin.BEndAddr){ //目标起始和结尾都在bin文件之外
            int FillCount = DstEnd - DstStart + 1;
            QByteArray tmp;
            tmp.fill(FillC, FillCount);
            ret.BinData.replace(0, FillCount, tmp);
        }
        else{ //其他
            ret.BinData.clear();
        }
    }

    else{ //其他
        ret.BinData.clear();
    }
    qDebug() << "ret.size(2)" << ret.BinData.size();

    if(Byte8Or16 == "TI"){  // Out转成Hex将ret的起始地址和结尾地址转换
        ret.BStartAddr = DstStart_Store;
    }
    return ret;
}

/**
* This function is used to convert QByteArray to QByteArray.
* @param[in]   QByteArray str = {0x31};
* @param[out] QByteArray ret = {'3', '1'};
* @retval
* @par Created in 2019-07-25
*/
QByteArray QByteArray2Hex(QByteArray str)
{
    QByteArray ret;
    for(int i = 0; i < str.size(); i++){
        char LSB = (str[i] >> 4) & 0x0f; //高4位
        char MSB = (str[i] & 0x0f);      //第4位
        ret.append( QByteArray::number(LSB, 16).toUpper() );
        ret.append( QByteArray::number(MSB, 16).toUpper() );
    }
    return ret;
}


/**
* This function is used to convert QString to QByteArray.
* @param[in]   QString str = "123456789";
* @param[out]  QByteArray ret = {0x12, 0x34, 0x56, 0x78, 0x09};
* @retval
* @par Created in 2020-06-05
*/
QByteArray QStringToChar(QString str)
{
    int j = 0;
    QByteArray res;
    while(str.size() != 0){
        while(str.size() < 2){str.insert(0,'0');}
        res.append(QStringToUint32(str.mid(0,2)));
        str.remove(0,2);
    }
    return res;
}

 

你可能感兴趣的:(Qt工程)