⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hexshowdoc.cpp

📁 以二进制的形式打开文件
💻 CPP
字号:
// HexShowDoc.cpp : implementation of the CHexShowDoc class
//

#include "stdafx.h"
#include "HexShow.h"

#include "HexShowDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc

IMPLEMENT_DYNCREATE(CHexShowDoc, CDocument)

BEGIN_MESSAGE_MAP(CHexShowDoc, CDocument)
	//{{AFX_MSG_MAP(CHexShowDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc construction/destruction

CHexShowDoc::CHexShowDoc()
{
	// TODO: add one-time construction code here
	m_nBytesPerLine = 16;//每行显示16个字节
	m_lFileLength  = 0L;
	m_pHexFile     = NULL;
}

CHexShowDoc::~CHexShowDoc()
{
	if (m_pHexFile != NULL){
		m_pHexFile->Close();
		delete m_pHexFile;
		m_pHexFile = NULL;
	}
}

BOOL CHexShowDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc serialization

void CHexShowDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc diagnostics

#ifdef _DEBUG
void CHexShowDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CHexShowDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHexShowDoc commands

BOOL CHexShowDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	// TODO: Add your specialized creation code here
	if (m_pHexFile != NULL){
		m_pHexFile->Close();
		delete m_pHexFile;
	}
	m_pHexFile = new CFile(lpszPathName,CFile::modeRead|CFile::typeBinary);

	if (!m_pHexFile){
		AfxMessageBox("文件打开错!");
		return FALSE;
	}
	m_lFileLength = m_pHexFile->GetLength();

	return TRUE;
}

BOOL CHexShowDoc::ReadFileAndProcess(CString &strLine, long lOffset)
{
	long lPos;
	if (lOffset != -1)
		lPos = m_pHexFile->Seek(lOffset,CFile::begin);
	else
		lPos = m_pHexFile->GetPosition();
	unsigned char szBuf[16];
	int nRet = m_pHexFile->Read(szBuf,m_nBytesPerLine);
	if (nRet <= 0)
		return FALSE;
	CString sTemp;
	CString sChars;
	sTemp.Format(_T("%8.8X: "),lPos);
	strLine = sTemp;
	for (int i=0; i<nRet; i++){
		if (i == 0)
			sTemp.Format(_T("%2.2X"),szBuf[i]);
		else if (i % 16 == 0)
			sTemp.Format(_T(" %2.2X"),szBuf[i]);
		else if (i % 8 == 0)
			sTemp.Format(_T(" - %2.2X"),szBuf[i]);
		else
			sTemp.Format(_T(" %2.2X"),szBuf[i]);
		if (_istprint(szBuf[i]))
			sChars += szBuf[i];
		else sChars += _T('.');
		strLine += sTemp;
	}//end for
	if (nRet < m_nBytesPerLine){
		CString sPad(_T(' '),2+3*(m_nBytesPerLine-nRet));
		strLine += sPad;
	}
	strLine += _T(" ");
	strLine += sChars;
	return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -