接上一个博客,hex转换成功了内存文件,但是无法验证转换的文件是否正确,然后目前有两种方法
第一种:将这种内存文件反转成Hex文件进行对比
第二种:将这种内存文件转换成Bin文件,然后再将原Hex文件转换成Bin文件,然后两个Bin文件进行对比、
(HEX和BIN文件的转换举例)
Hex文件:
:03000000020040BB
:0700400075815F000200431F
转换后的BIN文件:
02 00 40 FF FF 75 81 5F 00 02 00 43
实际就是讲HEX文件中的数据段以十六进制的方式保存到BIN文件中。
#include <QCoreApplication>
#include <stdio.h>
#include <stdlib.h>
#include<QDebug>
#include<QString>
#include<QByteArray>
#include<QFile>
#include<QDataStream>
static unsigned char pptr[16];
typedef struct falshpart
{
QString FlashPartHead;
QString FlashPartPartDate;
}FlashTOHex;
static FlashTOHex Flashtohex;
/****函数声明****************************/
int mystrlen(const char *StrDest);
unsigned char mystrlenchar(const char *StrDest);
int HexToDec(char *src);
unsigned char HexToDecchar(char *src);
FlashTOHex FormatHexjudge(QByteArray src);
QString AnalyseHEX(QByteArray dest);
void QString2intArray2(QString src);
void ReadWrite(QString filenameread,QString filenameWrite);
void ReadWriteToBIN(QString filenameread);
/****计算字符串长度*****/
int mystrlen(const char *StrDest)
{
int i;
i=0;
while((*StrDest++)!='\0')
{
i++;
}//这个循环体意思是从字符串第一个字符起计数,只遇到字符串结束标志'\0’才停止计数
return i;
}
/****计算字符串长度*****/
unsigned char mystrlenchar(const char *StrDest)
{
unsigned char i;
i=0;
while((*StrDest++)!='\0')
{
i++;
}//这个循环体意思是从字符串第一个字符起计数,只遇到字符串结束标志'\0’才停止计数
return i;
}
/****16进制转换10进制*****/
int HexToDec(char *src)
{
//将src中的无符号十六进制字符串转为十进制数
//如src="001A",则返回26
//字符串src需要以'\0'作为结束标志
int value=0,srclen=mystrlen(src);
int i;
for(i=0;i<srclen;i++)
{
if(src[i]<='9'&&src[i]>='0')
{
value=value*16+(int)(src[i]-'0'); //'0'
}
else if(src[i]<='f'&&src[i]>='a')
{
value=value*16+(int)(src[i]-'a'+10);
}
else
{
value=value*16+(int)(src[i]-'A'+10);
}
}
return value;//返回转换后的数值
}
/****16进制转换10进制*****/
unsigned char HexToDecchar(char *src)
{
//将src中的无符号十六进制字符串转为十进制数
//如src="001A",则返回26
//字符串src需要以'\0'作为结束标志
unsigned char value=0,srclen=mystrlenchar(src);
int i;
for(i=0;i<srclen;i++)
{
if(src[i]<='9'&&src[i]>='0')
{
value=value*16+(unsigned char)(src[i]-'0'); //'0'
}
else if(src[i]<='f'&&src[i]>='a')
{
value=value*16+(unsigned char)(src[i]-'a'+10);
}
else
{
value=value*16+(unsigned char)(src[i]-'A'+10);
}
}
return value;//返回转换后的数值
}
/**************************************
* 时间:06/17
* 函数名:FlashTOHex FormatHexjudge(QByteArray src)
* 返回值:分割后的字符串 头九个字节FlashPartHead(附带冒号),
* 和后边数据段FlashPartPartDate
*
* ***********************************/
FlashTOHex FormatHexjudge(QByteArray src)
{
QString dest;
static QByteArray space=" ";
//qDebug()<<src.size(); //大小带空格
Flashtohex.FlashPartHead=src.left(9);
dest=src.right(src.size()-9);
//dst=src.right(src.size()-9).simplified(); //清除两边的空格
Flashtohex.FlashPartPartDate=dest.remove(QRegExp("\\s")); //清除中间多余的空格
//qDebug()<<Flashtohex.FlashPartHead;
//qDebug()<<Flashtohex.FlashPartPartDate;
return Flashtohex;
}
/**0617**字符串效验*****/
QString AnalyseHEX(QByteArray dest)
{
unsigned char i=1;
int tempsum=0; //必须初始化,否则乱码
QString stype;
QString ZERO="0";
QString Date0x100="100";
QString result;
do
{
stype = dest.mid(i,2); //转换成10进制运算后,再转换成十六进制
printf(" %d",HexToDec(stype.toLatin1().data()));
tempsum+=HexToDec(stype.toLatin1().data());
i+=2;
} while (i<(mystrlen(dest))); //和效验那一块有点不一样
/***HEX计算公式(0x100-(16进制数据和后两位))再取后两位=字符串后两位*****/
result=QString::number((HexToDec(Date0x100.toLatin1().data())-HexToDec
(QString::number(tempsum,16).right(2).toLatin1().data())),16);
if(result.size()<2)
{
int count=2-result.size();
//printf("count=2-result.right(2).size()=%d\n",count);
while(count-->0)
{
result.insert(0,ZERO);
}
}
//qDebug()<<"result.right(2).toUpper()"<<result.right(2).toUpper();
return result.right(2).toUpper();
}
/****0617**新字符串拼接**************************/
QString FormatTranQARRYbyte(FlashTOHex Flashtohex)
{
QString Dest;
QString DateSize;
static QString BASEaddress;
static QString BASEdate;
//if(Flashtohex.FlashPartHead.at(9)!=":") //判断第九个字符是否为:
//qDebug()<<Flashtohex.FlashPartPartDate.size()/2;
DateSize=QString::number(Flashtohex.FlashPartPartDate.size()/2,16);
if(DateSize.size()<2)
{
int numcount=2-DateSize.size();
while(numcount-->0)
{
DateSize.insert(0,"0");
}
}
Dest.insert(0,":");
//qDebug()<<"DateSize"<<DateSize;
Dest.insert(1,DateSize);
Dest.insert(3,Flashtohex.FlashPartHead.mid(4,4));
Dest.insert(7,"00");
Dest.insert(9,Flashtohex.FlashPartPartDate);
//qDebug()<<Dest.toUtf8()<<endl;
//qDebug()<<"AnalyseHEX(Dest.toUtf8())"<<AnalyseHEX(Dest.toUtf8())<<endl;
Dest.insert(9+Flashtohex.FlashPartPartDate.size(),AnalyseHEX(Dest.toUtf8()));
//qDebug()<<Dest<<endl;
//system("pause");
return Dest.toUpper();
}
/*****将字符串转换成十进制存储到pptr数组中******************/
void QString2intArray2(QString src)
{
unsigned char tranfer;
int count=0;
int coun=0;
//qDebug()<<"Pcount"<<src.size();
while(count<src.size())
{
tranfer=HexToDecchar(src.mid(count,2).toLatin1().data()); //先转换成16进制
pptr[coun++]=tranfer;
count+=2;
//qDebug()<<"tranfer"<<tranfer;
}
}
/**0617*按行读取HEXVIEW文件并进行处理再转回Hex文件*****/
void ReadWrite(QString filenameread,QString filenameWrite)
{
QString EnterResul="\n";
QFile fileread(filenameread);
if(!fileread.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << " Could not open the file for reading";
return;
}
QFile filewrite(filenameWrite);
if(!filewrite.open(QFile::WriteOnly | QFile::Text))
{
qDebug() << " Could not open the file for reading";
return;
}
QTextStream in(&fileread);
while(!in.atEnd())
{
QString myText = in.readLine();
QString str(myText);
QByteArray bytes = str.toLatin1(); //Qstring 转 QByteArray
//qDebug() << bytes;
Flashtohex=FormatHexjudge(bytes);
QString ResultText;
ResultText=FormatTranQARRYbyte(Flashtohex);
filewrite.write(ResultText.toUtf8()); //写入数据
filewrite.write(EnterResul.toUtf8()); //写入回车
}
fileread.close();
filewrite.close();
}
/******QStringToChar*QString转换成字符数组*************/
// unsigned char* Qstring2charArray(QString src)
// {
// int Pcount=0;
// bool ok;
// int num;
// static unsigned char Date[]={0};
// while(src.size()-Pcount>0)
// {
// num=src.mid(Pcount,2).toInt(&ok,16);
// qDebug() <<"num"<<num;
// qDebug() <<"QString::number"<<QString::number(num,16);
// //Date[Pcount]=(unsigned char)(QString::number(num,16)).toLatin1().data();
// //fs=QString::number(num,16);
// //src.mid(Pcount,2).toLatin1().data();
// Pcount+=2;
// }
// return Date;
// }
///*********写bin文件(测试代码)**************************/
//int hextobin()
//{
// FILE * pFile;
// static unsigned char Date[]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x7F,0xFF,0xFF,};
// //char buffer[] = {'x','y','z'};
// unsigned char Dates[]="FFFFFFFFFFFFFFFFFFFFFFFFFE7FFFFF";
// pFile = fopen ("myfile.bin", "wb");
// fwrite (Dates , sizeof(unsigned char), sizeof(Date), pFile);
// fclose (pFile);
// return 0;
//}
/*****************************************
* 时间:2019、6、19
* *0619**写数据到BIN文件(大量)
* 形参:输入待转换的文件夹名(是hextoview后的文件),转换后的BIN文件
* 将被存储到mybin.bin文件中
*
*******************************************/
void ReadWriteToBIN(QString filenameread)
{
QString EnterResul="\n";
FlashTOHex FlashDate;
FILE * pWriteFile;
QFile fileread(filenameread);
if(!fileread.open(QFile::ReadOnly | QFile::Text))
{
qDebug() << " Could not open the file for reading";
return;
}
pWriteFile = fopen ("myfile.bin", "wb");
QTextStream in(&fileread);
while (!in.atEnd())
{
unsigned long long pptrsize=0;
QString myText = in.readLine(); //按行读一行文件
FlashDate=FormatHexjudge(myText.toLatin1()); //分割 此处数据段后应该没有效验值
pptrsize=(unsigned long long)(FlashDate.FlashPartPartDate.size())/2; //计算数组的有效长度
QString2intArray2(FlashDate.FlashPartPartDate);
//qDebug() <<"pptrsize"<<pptrsize;
fwrite (pptr ,1, pptrsize, pWriteFile); //把每次数组里的文件存储到BIN文件中
//system("pause");
}
fclose (pWriteFile);
}
///******将字符串写入BIN文件*(测试2)**********/
//int hextobin(const unsigned char *Dates)
//{
// FILE * pFile;
// //static unsigned char Date[]={0,16,0,0,17,4,0,0}; //,255,255,255,255,254,247,255,255
// pFile = fopen ("myfile.bin", "wb");
// qDebug()<<"Date[]"<<sizeof(Dates);
// fwrite (Dates ,1, sizeof(Dates), pFile);
// fclose (pFile);
// return 0;
//}
int main()
{
QString filename = "C:/Users/Administrator/Desktop/HexViewtobin/hexrecver/Pro1.hex";
QString filewritename = "C:/Users/Administrator/Desktop/HexViewtobin/hexrecver/Prohex.hex";
//ReadWrite(filename,filewritename); //反转成HEX文件,目前没有加地址扩展
ReadWriteToBIN(filename); //使用HEXVIEW转换成BIN文件
qDebug()<<"ok!";
}
从下一篇文章我就更新到自己的github 和 码云了