请教:mfc对话框怎样读取HEX文件中的数据并保存在数组f[j]?

RobinChow_ 2014-08-06 05:59:14
困扰了两天,编的代码可以读文本文档但是不能读.hex文件,
.hex格式如下:

想要读取文件中的D4 FF 48等数字,
请问应该怎样编写呢?
之前的代码是这样的:



期待您的回答,多谢了
...全文
1110 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈希诺 2014-12-10
  • 打赏
  • 举报
回复
你好,LZ你解决了这个问题吗?
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
void CIExample0042aDlg::OnButton1() //打开文件按钮 { CFile binfile; binfile.Open( m_CyrrentFileName,CFile::modeRead|CFile::shareExclusive); ReadBin(&binfile); binfile.Close(); StringBin(); }
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
// doc public: CStringArray m_HexStrArray; int m_nSize; BYTE *m_pBuffer; CString m_CyrrentFileName; // view protected: CFont* m_pFont; BOOL m_bModified;
RobinChow_ 2014-08-07
  • 打赏
  • 举报
回复
4楼您好,我是新手,还是没有懂。 根据您的建议,大体要这样写吗?

void CIExample0042aDlg::OnButton1() //打开文件按钮
{
	LPCTSTR lpszPathName;
	CString m_CyrrentFileName;
 m_CyrrentFileName=lpszPathName;  
 CFile binfile;  
 binfile.Open(lpszPathName,CFile::modeRead|CFile::shareExclusive);  
 ReadBin(&binfile);   
 binfile.Close();      
 StringBin(); 
}


BOOL CIExample0042aDlg::ReadBin(CFile *pBinFile) 
{  
	m_nSize =pBinFile->GetLength();    
 if(m_nSize==0)    
 {       
	 MessageBox("Binary file has 0 size !");  
	 return FALSE;    
 }   
 if(m_pBuffer) 
	 delete []m_pBuffer;   
 m_pBuffer=new BYTE[m_nSize];   
 if(m_pBuffer==0)    
 {        
	 MessageBox("Not enough memory !");  
	 return FALSE;        
 }   
 pBinFile->ReadHuge(m_pBuffer,m_nSize);    
 return TRUE; 
}   
BOOL CIExample0042aDlg::StringBin() 
{     
	BYTE *pTmp=m_pBuffer;   
	CString tmp;    
	CString prompt;   
	char m_HexStrArray;
	BYTE comment[40];    
	m_HexStrArray.RemoveAll();          //编译时提示没有remove all()函数  
	int FullRows=m_nSize/16;   
	int PartRow=m_nSize%16; 
	for(int rr=0;rr < FullRows ; rr++)  
	{     
		tmp.Format("%05X:", 8*rr);//"10000:"  8Words    
		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);     
		prompt.Empty();   
	} 
    if(PartRow!=0)  
	{      
		tmp.Format("%05X:", 8*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)     
		{
			prompt+="   ";//3   
			col++;   
	}     
		prompt+="    ";  
		prompt+=comment;  
		prompt+="\r\n";    
		m_HexStrArray.Add(prompt);    //编译时提示没有add()函数  
		prompt.Empty();     
	}   
	return TRUE; 
}
提示有好多错误 急死我了
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
你这个不叫 hex文件, 另有叫Hex的文件,(是写CPU flash)的. 你就是一个 二进制文件没按 Hex 格式显示. 所以: 1 打开文件,读入二进制码到内存. ReadBin(&binfile); 2, 把这个二进制文件 按格式 变成 字符串. StringBin() 3. 最后就是显示: void CShowHexView::ShowArray(CStringArray& strArray) { CWaitCursor wait; // Clean old GetEditCtrl().SetWindowText(""); // Get total int rows=strArray.GetSize(); if(rows==0) return; // GetEditCtrl().LockWindowUpdate(); CString prompt; int nStartChar,nEndChar; // From start GetEditCtrl().SetSel(0,0); for(int i=0;i<rows;i++) {// Fill all prompt=strArray.ElementAt(i); GetEditCtrl().GetSel(nStartChar,nEndChar); GetEditCtrl().SetSel(nEndChar,nEndChar); GetEditCtrl().ReplaceSel(prompt); } GetEditCtrl().SetSel(0,0); GetEditCtrl().UnlockWindowUpdate(); // Otherwise you will be asked to save ... SetModify(FALSE);//ours // do not use : // GetEditCtrl().SetModify(FALSE); }
RobinChow_ 2014-08-07
  • 打赏
  • 举报
回复
多谢楼上了, 但楼上所说的可能是文档环境下的hex打开方法,怎样用对话框实现打开hex文件呢?
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
已上传到我的资源. "ShowHex.zip" 0 分 http://download.csdn.net/detail/schlafenhamster/7726787 "本程序 类似 UE 或 WinHex, 用于显示二进制文件的 Hex 格式."
RobinChow_ 2014-08-07
  • 打赏
  • 举报
回复
非常感谢您,能不能把这个工程发给我啊,头有点大,想好好看看,多谢了 qq:1689980829
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
public: CStringArray m_HexStrArray;// 是最后的字符串; 显示就是用的这个 // “读出来的是字符串还是数字? ” 在 BYTE *m_pBuffer; 里 ; 是字节
schlafenhamster 2014-08-07
  • 打赏
  • 举报
回复
RobinChow_ 2014-08-07
  • 打赏
  • 举报
回复
还提示没有.add和removeall()

下面这段代码是什么意思?
int FullRows=m_nSize/16;   
int PartRow=m_nSize%16;
for(int rr=0;rr < FullRows ; rr++)
{
tmp.Format("%05X:", 8*rr);//"10000:" 8Words
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);
prompt.Empty();
}
if(PartRow!=0)
{
tmp.Format("%05X:", 8*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)
{
prompt+=" ";//3
col++;
}
prompt+=" ";
prompt+=comment;
prompt+="\r\n";
m_HexStrArray.Add(prompt); //编译时提示没有add()函数
prompt.Empty();
}
return TRUE;

读出来的是字符串还是数字?存到哪里去了?
谢谢大侠了
schlafenhamster 2014-08-06
  • 打赏
  • 举报
回复
BOOL CShowHexDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
// do not call base
// if (!CDocument::OnOpenDocument(lpszPathName)) return FALSE;

// TODO: Add your specialized creation code here
m_CyrrentFileName=lpszPathName;
CFile binfile;
binfile.Open(lpszPathName,CFile::modeRead|CFile::shareExclusive);
ReadBin(&binfile);
binfile.Close();
// show
StringBin();
//
return TRUE;
}

//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;
}

// 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++)
{//
tmp.Format("%05X:", 8*rr);//"10000:" 8Words
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);
prompt.Empty();
}
//
if(PartRow!=0)
{
tmp.Format("%05X:", 8*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);
prompt.Empty();
}
//
return TRUE;
}
RobinChow_ 2014-08-06
  • 打赏
  • 举报
回复


16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧