求vc读HEX文件并转换成二进制bin文件的代码
求一个用VC写的程序打开一个.HEX格式的文件,然后转换成二进制BIN格式的文件(要求没64K限制),并用下面的EncTable用查表法加密代换,再保存为.BIN文件En...
求一个用VC写的程序
打开一个.HEX格式的文件,然后转换成二进制BIN格式的文件(要求没64K限制),
并用下面的EncTable用查表法加密代换,再保存为.BIN文件
EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C...............};
比如a.bin文件里的数据是:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
.......
用查表法加密代换后变成
76 3F D9 E4 BB 92 DF F4 F2 AF 97 34 E7 A8 20 F3
C3 BE A1 B8 41 38 8B 59 26 94 74 96 A5 A6 C7 EA
......
谢谢!速度快的,效果好的再加分,谢谢! 展开
打开一个.HEX格式的文件,然后转换成二进制BIN格式的文件(要求没64K限制),
并用下面的EncTable用查表法加密代换,再保存为.BIN文件
EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C...............};
比如a.bin文件里的数据是:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
.......
用查表法加密代换后变成
76 3F D9 E4 BB 92 DF F4 F2 AF 97 34 E7 A8 20 F3
C3 BE A1 B8 41 38 8B 59 26 94 74 96 A5 A6 C7 EA
......
谢谢!速度快的,效果好的再加分,谢谢! 展开
3个回答
展开全部
首先需要完善EncTable里的字节内容。(我估计这个EncTable里的字节应该是256个不重复的字节,要不密文就还原不了了。)
const unsigned char EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C
};
const int N=1024; //我每次从文件中读1024个字节进行转换,你可以根据需要适当调节。
// strHexPathName是你要转换的Hex文件路径字符串(包括路径,文件名和扩展名)
// strBinPathName是转换后的Bin文件路径字符串(包括路径,文件名和扩展名)
void Convert(LPCTSTR strHexPathName,LPCTSTR strBinPathName)
{
CFile fileHex;
CFile fileBin;
if(!fileHex.Open(strHexPathName,CFile::modeRead|CFile::typeBinary)){
return;
}
if(!fileBin.Open(strBinPathName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){
fileHex.Close();
return;
}
unsigned char pBufRead[N]={0};
unsigned char pBufWrite[N]={0};
int nReadLen=0;
do{
nReadLen=fileHex.Read(pBufRead,N);
if(nReadLen>0){
Encrypt(pBufRead,pBufWrite,nReadLen);
}
fileBin.Write(pBufWrite,nReadLen);
}while(nReadLen>0);
fileBin.Close();
fileHex.Close();
}
void Encrypt(unsigned char* pInBuf,unsigned char* pOutBuf,int nLength)
{
for(int i=0;i<nLength;i++){
pOutBuf[i]=EncTable[pInBuf[i]];
}
}
const unsigned char EncTable[256] =
{
0x76,0x3F,0xD9,0xE4,0xBB,0x92,0xDF,0xF4,0xF2,0xAF,0x97,0x34,0xE7,0xA8,0x20,0xF3,
0xC3,0xBE,0xA1,0xB8,0x41,0x38,0x8B,0x59,0x26,0x94,0x74,0x96,0xA5,0xA6,0xC7,0xEA,
0x16,0x3C,0x4E,0x88,0xD3,0x19,0x75,0x9F,0x03,0x15,0x54,0x42,0x8A,0xD7,0xCC,0x5A,
0xD8,0xBC,0x43,0x00,0x5D,0xA2,0xF5,0xFA,0x40,0xC8,0x65,0xC2,0xCF,0x72,0xA4,0xFC,
0xFB,0x77,0x33,0xF8,0x1B,0xF9,0xB3,0x80,0xF7,0x3D,0x45,0x9C,0xAB,0xE8,0xE1,0x58,
0x3A,0xCE,0x2C
};
const int N=1024; //我每次从文件中读1024个字节进行转换,你可以根据需要适当调节。
// strHexPathName是你要转换的Hex文件路径字符串(包括路径,文件名和扩展名)
// strBinPathName是转换后的Bin文件路径字符串(包括路径,文件名和扩展名)
void Convert(LPCTSTR strHexPathName,LPCTSTR strBinPathName)
{
CFile fileHex;
CFile fileBin;
if(!fileHex.Open(strHexPathName,CFile::modeRead|CFile::typeBinary)){
return;
}
if(!fileBin.Open(strBinPathName,CFile::modeCreate|CFile::modeWrite|CFile::typeBinary)){
fileHex.Close();
return;
}
unsigned char pBufRead[N]={0};
unsigned char pBufWrite[N]={0};
int nReadLen=0;
do{
nReadLen=fileHex.Read(pBufRead,N);
if(nReadLen>0){
Encrypt(pBufRead,pBufWrite,nReadLen);
}
fileBin.Write(pBufWrite,nReadLen);
}while(nReadLen>0);
fileBin.Close();
fileHex.Close();
}
void Encrypt(unsigned char* pInBuf,unsigned char* pOutBuf,int nLength)
{
for(int i=0;i<nLength;i++){
pOutBuf[i]=EncTable[pInBuf[i]];
}
}
更多追问追答
追问
您好,EncTable里的字节内容是因为发表提问字数的原因而省略掉一些;
您给的程序还不是完整的,我在VC里修改后还是老提示错误,请问如果可以的话能不能把完整的代码发给我,谢谢
djohn@21cn.com
追答
贴出的代码我只是列出了主要的函数,稍后我会把工程文件给你,是vs2010的。另外,有个地方我可能理解错了,你提到的hex文件是用文本方式来保存16进制还是和bin文件一样也是2进制文件?我是按后者写的。
2013-09-22
展开全部
留下个联系方式嘛,要了解你的HEX格式文件是怎样的
追问
您好,HEX格式是这样的
:0380800000A0005D
:0C83E80001020408010204080A10142815
:0483F4000000006421
:2083F800AE17FF9490CE8083AE8085F62720A5602717BF91EE03BF94BE91EE0190F6F75CCE
比如第一行 在地址8080写 00 A0 00
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-22
展开全部
首先需要完善EncTable里的字节内容。(我估计这个EncTable里的字节应该是256个不重复
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询