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

📄 hexshowview.cpp

📁 以二进制显示数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// HexShowView.cpp : implementation of the CHexShowView class
//

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

#include "input.h"
#include "Look.h"
#include "modify.h"
#include "gbkunicode.h"
#include "unicodegbk.h"
#include "datamove.h"
#include "SaveData.h"

#include "HexShowDoc.h"
#include "HexShowView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CHexShowView

IMPLEMENT_DYNCREATE(CHexShowView, CScrollView)

BEGIN_MESSAGE_MAP(CHexShowView, CScrollView)
	//{{AFX_MSG_MAP(CHexShowView)
	ON_COMMAND(AFX_ID_FILE_DOWN, OnIdFileDown)
	ON_COMMAND(AFX_ID_FILE_UP, OnIdFileUp)
	ON_COMMAND(AFX_ID_FILE_ZHIZHEN, OnIdFileZhizhen)
	ON_COMMAND(AFX_ID_FILE_LOOK, OnIdFileLook)
	ON_COMMAND(AFX_ID_FILE_MODIFY, OnIdFileModify)
	ON_COMMAND(AFX_ID_CHARACTER_CODE, OnIdCharacterCode)
	ON_COMMAND(AFX_ID_CODE_CHARACTER, OnIdCodeCharacter)
	ON_COMMAND(AFX_ID_DATA_MOVE, OnIdDataMove)
	ON_COMMAND(AFX_ID_DATA_SAVE, OnIdDataSave)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHexShowView construction/destruction

CHexShowView::CHexShowView()
{
	// TODO: add construction code here
	// 选择字体

	nLine=0;
	LOGFONT m_logfont;
	memset(&m_logfont,0,sizeof(m_logfont));
	_tcscpy(m_logfont.lfFaceName,_T("Fixedsys"));
	CClientDC dc(NULL);
	m_logfont.lfHeight=::MulDiv(120,dc.GetDeviceCaps(LOGPIXELSY),720);
	m_logfont.lfPitchAndFamily=FIXED_PITCH;
	m_pFont =new CFont;
	m_pFont->CreateFontIndirect(&m_logfont);

}

CHexShowView::~CHexShowView()
{
	if (m_pFont !=NULL)
		delete m_pFont;
}

BOOL CHexShowView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CHexShowView drawing

void CHexShowView::OnDraw(CDC* pDC)
{
	CHexShowDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (pDoc->m_pHexFile==NULL)
		return;


	int n_Line=0;
 	int nHeight=20;
	CString sLine;
	CFont* pOldFont;
	// TODO: add draw code for native data here
//每次重写屏时清除,保证每次保存的是当前页

	pDoc->font_tag=0;//每次重写屏,保证“上一次是办个汉字的标志清楚”


	pDoc->m_SaveFileData="           0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F";
	pOldFont=pDC->SelectObject(m_pFont);
	pDC->TextOut(0,0,"           0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F");

	if (pDoc->m_pHexFile!=NULL)
	{

//*****************************************************

		while(n_Line<30)
		{
			if (!pDoc->ReadFileAndProcess(sLine,(n_Line+nLine)*16))
				break;
			pDC->TextOut(0,nHeight,sLine);
			nHeight=nHeight+15;
			n_Line++;
		}
	
//*********************************************************	
	
	}
	pDC->SelectObject(pOldFont);


}

void CHexShowView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();
	CHexShowDoc* pDoc=GetDocument();
	ASSERT_VALID(pDoc);
	CSize sizeTotal(0,pDoc->m_lFileLength);

	//CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 100;
	SetScrollSizes(MM_TEXT, sizeTotal);
}

/////////////////////////////////////////////////////////////////////////////
// CHexShowView printing

BOOL CHexShowView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CHexShowView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CHexShowView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CHexShowView diagnostics

#ifdef _DEBUG
void CHexShowView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CHexShowView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CHexShowDoc* CHexShowView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHexShowDoc)));
	return (CHexShowDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHexShowView message handlers



void CHexShowView::OnIdFileDown() 
{
	// TODO: Add your command handler code here
nLine=nLine+30;	
OnUpdate(NULL,NULL,NULL);
}

void CHexShowView::OnIdFileUp() 
{
if(nLine<30)
	{nLine=30;
	return;
	}
nLine=nLine-30;	// TODO: Add your command handler code here
OnUpdate(NULL,NULL,NULL);	
}

void CHexShowView::OnIdFileZhizhen() 
{
	// TODO: Add your command handler code here
	CHexShowDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

     long filelength;
	CString   str;
	CString    file_zhizhen;
	filelength=	pDoc->m_lFileLength;
	str.Format("%d",filelength);









	Cinput dlg;
	nLine=nLine*16;
	
	dlg.m_zhizhen=nLine;
	dlg.m_file_length =str;


	str.Format("%X",filelength);
	dlg.m_file_lengthHEX =str;




	int nResponse = dlg.DoModal();
	if (nResponse == IDCANCEL)
		{return;
		}
	
	str=dlg.m_zhizhenHEX;
	//AfxMessageBox(str);
	if (Chex_long(str)<=0)
	{nLine=	dlg.m_zhizhen;
	}
	else
	{
	nLine=Chex_long(str);
	}
	

//	str.Format("%d",nLine);
//	AfxMessageBox(str);

	//file_zhizhen	
	nLine=int(nLine/16);
	
	OnUpdate(NULL,NULL,NULL);	
}
//将字符16进制转成LONG格式
long CHexShowView::  Chex_long(CString  str)//=-1表示出错
{
    long  shuzi=0;
	int  str_length;//用于文件长度的数值长度<=8,大于8出错
	int  str_shuzi_int;//用于保存从字符中读出数的ASCII码
	long  str_shuzi[8];//用于保存从字符中读出数的ASCII码
	char  str_char;//从文件长度数值中读出字
	CString  sstr;//临时使用
	int I=0;//临时使用
	while (I<8)
	{
		str_shuzi[I]=0;
		I++;
	}
	
	
	str.TrimLeft();
	str.TrimRight();
	str.Replace(" ","");
	str.MakeUpper();
	str_length=str.GetLength();


    
	if (str_length>8)
		return  -1;
	if (str_length==0)
		return  0;	
	I=0;
	while (I<str_length)
	{	str_char=str.GetAt(str_length-1-I);
		str_shuzi_int=str_char;

	//sstr.Format("%d",str_shuzi_int);
	//AfxMessageBox(sstr);

		if  (str_shuzi_int<48||(str_shuzi_int>57&&str_shuzi_int<65)||str_shuzi_int>70)
		{	return  -1;
		}
		if  (str_shuzi_int>=48&&str_shuzi_int<=57)
		{	str_shuzi_int=str_shuzi_int-48;
		}
		else
		{	str_shuzi_int=str_shuzi_int-55;
		}
		str_shuzi[I]=str_shuzi_int;
	
	
	I++;
}
if(str_shuzi[7]>7)
{
	return  -1;
}
str_shuzi[7]=str_shuzi[7]*16*16*16*16*16*16*16;	
str_shuzi[6]=str_shuzi[6]*16*16*16*16*16*16;	
str_shuzi[5]=str_shuzi[5]*16*16*16*16*16;	
str_shuzi[4]=str_shuzi[4]*16*16*16*16;	
str_shuzi[3]=str_shuzi[3]*16*16*16;	
str_shuzi[2]=str_shuzi[2]*16*16;	
str_shuzi[1]=str_shuzi[1]*16;	
str_shuzi[0]=str_shuzi[0];	
	
shuzi=str_shuzi[7]+str_shuzi[6]+str_shuzi[5]+str_shuzi[4]+str_shuzi[3]+str_shuzi[2]+str_shuzi[1]+str_shuzi[0];

return shuzi  ;
}
//******************************************************************************
int * CHexShowView:: Chex_int_array(CString  str)//[0]=-1错
{
	
	int  szBuf_hex[20];//用于从字符串CSTRING中读出字符//
	//int  szBuf_hex_char[10];//用于保存变换后的数值
	int  szBuf[10];//用于返回数值,
	int char_h;
	int char_l;
//	int char_hex;
	int k=0;
    int data_length;
	int i;
	
	str.MakeUpper();
	data_length=str.GetLength();
	k=int(data_length/2);
	if(2*k!=data_length)//长度不是偶数,
	{
		szBuf[0]=-1;
		return  szBuf;
	}
	if(k>10)//长度太长
	{
		szBuf[0]=-1;
		return  szBuf;
	}
	
	i=0;
	while(i<data_length)
		{	
			szBuf_hex[i]=str.GetAt(i);
			i++;
		}

			i=0;
			int j=0;
			while(i<k*2)
				{
				char_h=szBuf_hex[i];
				if  (char_h<48||(char_h>57&&char_h<65)||char_h>70)
					{	
						szBuf[0]=-1;
						return  szBuf;
					}
				if  (char_h>=48&&char_h<=57)
					{	char_h=char_h-48;
					}
				else
					{	char_h=char_h-55;
					}

				char_l=szBuf_hex[i+1];
				if  (char_l<48||(char_l>57&&char_l<65)||char_l>70)
					{	
						szBuf[0]=-1;
						return  szBuf;
					}
		
				if  (char_l>=48&&char_l<=57)
					{	char_l=char_l-48;
					}
				else
					{	char_l=char_l-55;
					}



				szBuf[j]=char_h*16+char_l; 		

					j++;
					i=i+2;
				}	

	return szBuf;
}

void CHexShowView::OnIdFileLook() 
{
	// TODO: Add your command handler code here
	CHexShowDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(pDoc->m_pHexFile==NULL)
		{
		AfxMessageBox("文件未打开");
		return;}

	CString  str;
	long lOffset=0;//用于查找时的文件指针
	long  filelength;//文件长度
	int i=0;//用于临时使用
	int szBuf[10];
	unsigned char filedata;
	
	int  look_length=0;//输入待查找的字符的长度
	long m_ten_end;
	long m_ten_begin;
	
	int * p_modify;
	int k=0;


	
	CString  look_data;
	CString  look_data_hex;
	
	CLook dlg;
//**************************************************	

	filelength=	pDoc->m_lFileLength;
	
	str.Format("%d",filelength);
	dlg.m_file_length =str;
	
	str.Format("%x",filelength);
	dlg.m_file_length_hex =str;



	int nResponse = dlg.DoModal();
	if (nResponse == IDCANCEL)
		{return;
		}


	
	if (Chex_long(dlg.m_hex_end)>0)
	{
	
	
	m_ten_end  =Chex_long(dlg.m_hex_end);
	m_ten_begin=Chex_long(dlg.m_hex_begin);
 
	}
	else
	{
	m_ten_end  =	dlg.m_ten_end;
	m_ten_begin=	dlg.m_ten_begin;
 
	}


	if (m_ten_begin<0||m_ten_end<=0) 
		{	AfxMessageBox("查找范围数据错!!!"); 	
			return;
		}
	

	
	if (m_ten_begin>=m_ten_end) 
		{	AfxMessageBox("查找范围 开始>结束 !!!"); 	
			return;
		}

//********************************************************
	look_data=	dlg.m_look;//查找文本
	look_data_hex=	dlg.m_look_Hex;//查找十六进制内容
	
	look_data.TrimLeft();
	look_data.TrimRight();
	look_data.Replace(" ","");
	
	

	look_data_hex.TrimLeft();
	look_data_hex.TrimRight();
	look_data_hex.Replace(" ","");
	look_data_hex.MakeUpper();

	if (look_data.GetLength()<=0&&look_data_hex.GetLength()<=0)
	{	AfxMessageBox("没有输入查找内容!!!"); 
		return;
	}
	if (look_data.GetLength()>0&&look_data_hex.GetLength()>0)
	{	AfxMessageBox("请不要在查找内容两种输入框中输入内容!!!"); 
		return;
	}


	k=look_data_hex.GetLength();


	
	if( k>0)//如果十六进制输入框中数据不为0则,按照十六进制值查找
{
//**************************将文本形式的16进制转成字符************
//	look_data_hex

	if (k>20||k<=0)
    {	
		AfxMessageBox("数据长度不对!!!");
		return;
	}

	if (int(k/2)*2!=k)
	{
		AfxMessageBox("数据长度不是偶数!!!");
		return;

	}
	
	look_length=int(k/2);
	


	p_modify=Chex_int_array(look_data_hex);
	i=0;
	while(i<k/2)
	{
		szBuf[i]=*p_modify;
		p_modify++;
		i++;
	}
	if(szBuf[0]<0)
	{
		AfxMessageBox("数据出错!!");
		return;

⌨️ 快捷键说明

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