2,586
社区成员
发帖
与我相关
我的任务
分享
CString fileName = getModulePath() + "\\123.hex";
char strfile[64];
//strfile = new char[64];//存储从文件读取的数据
char strhex[128];
CFile hexfile(fileName, CFile::modeRead);//构造CFile对象
int i = 0;
hexfile.SeekToBegin();
int a = 0;
while(hexfile.Read(strfile, 64) != 0)//读出的文件不为空
{
int len = strlen(strfile); //len为什么等于75
for (a = 0; a < len; a++)
{
sprintf(strhex + 2*a, "%02X", (unsigned char)strfile[a]);
}
strFileArr.Add(strhex);
i++;
hexfile.Seek(64 * i, CFile::begin);//定位文件指针
}
//delete strfile;
hexfile.Close();//关闭文件
//Read Binary File into Buffer
BOOL CShowHexDoc::ReadBin(CFile *pBinFile)
{
m_nSize =pBinFile->GetLength();
if(m_nSize==0)
{
AfxMessageBox("Binary file has 0 size !",MB_ICONSTOP);
return FALSE;
}
//
if(m_pBuffer) delete []m_pBuffer;
//
m_pBuffer=new BYTE[m_nSize];
if(m_pBuffer==0)
{
AfxMessageBox("Not enough memory !",MB_ICONSTOP);
return FALSE;
}
pBinFile->ReadHuge(m_pBuffer,m_nSize); //
//
return TRUE;
}
BOOL CShowHexDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
// do not call base
// if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE;
// TODO: Add your specialized creation code here
m_CurrentFileName=lpszPathName;
CFile binfile;
binfile.Open(lpszPathName,CFile::modeRead|CFile::shareExclusive);
ReadBin(&binfile);
binfile.Close();
// show
StringBin();
//
return TRUE;
}
// Fill a string array in hex format
BOOL CShowHexDoc::StringBin()
{
BYTE *pTmp=m_pBuffer;
CString tmp;
CString prompt;
BYTE comment[40];
// if is there an old
m_HexStrArray.RemoveAll();
//
int FullRows=m_nSize/16;
int PartRow=m_nSize%16;
//
for(int rr=0;rr < FullRows ; rr++)
{//
prompt.Empty();
tmp.Format("%06X:", 16*rr);//"1000000:" 16 bytes
prompt+=tmp;
for(int col = 0;col < 16; col++)
{
if((*pTmp>' ') && (*pTmp<'z')) comment[col]=*pTmp;
else comment[col]='.';
tmp.Format("%02X ", *pTmp++);
prompt+=tmp;
}
comment[col]=0;
prompt+=" ";
prompt+=comment;
prompt+="\r\n";
m_HexStrArray.Add(prompt);
}
// afxDump << prompt ;// 0x383FFF
if(PartRow!=0)
{
prompt.Empty();
tmp.Format("%06X:", 16*rr);
prompt+=tmp;
for(int col = 0;col < PartRow; col++)
{
if((*pTmp>' ') && (*pTmp<'z')) comment[col]=*pTmp;
else comment[col]='.';
tmp.Format("%02X ", *pTmp++);
prompt+=tmp;
}
comment[col]=0;
while(col<16)
{// more spaces
prompt+=" ";//3
col++;
}
prompt+=" ";
prompt+=comment;
prompt+="\r\n";
m_HexStrArray.Add(prompt);
}
//
return TRUE;
}