读一个文件,格式如十六进制编辑器
bookaa的代码
#include "stdio.h"
void printhex_off(DWORD off, void* p, int len, I_MsgBack* pfn)
{
char s[17];
s[16] = 0;
PBYTE pbyte = (PBYTE)p;
while (len)
{
pfn->printf("%04x ", off >> 4 << 4);
for (int i=0; i<16; i++)
{
if (i == 8)
pfn->printf(" ");
if (off % 16 != i)
{
pfn->printf(" ");
s[i] = ' ';
continue;
}
if (len)
{
BYTE b = *pbyte++;
pfn->printf("%02x ", b);
len--;
s[i] = GetVisibleChar(b);
}
else//len==0
{
pfn->printf(" ");
s[i] = ' ';
}
off++;
}
pfn->printf(" %s/r/n", s);
}
pfn->printf("/r/n");
}
我的代码
头文件:
#include "iostream.h"
#include "afx.h"
class CRead
{
public:
CRead(char *filename);
~CRead();
void PrintToScreen();
private:
char GetVisibleChar(BYTE byte);
BYTE *p_Buf;
int length;
int offset;
};
源文件
#include "CReadHex.h"
CRead::CRead(char *filename)//构造
{
CFile myfile;
myfile.Open(filename,CFile::modeRead);
length=myfile.GetLength();
p_Buf=new BYTE[length];
myfile.Read(p_Buf,length);
myfile.Close();
offset=0;
}
CRead::~CRead()//析构
{
delete []p_Buf;
p_Buf=NULL;
}
char CRead::GetVisibleChar(BYTE byte)
{
if (byte>=48 && byte<=122)
{
return (char)byte;
}
else
return '.';
}
void CRead::PrintToScreen()
{
char s[17];
s[16] = 0;
BYTE *p_temp=p_Buf;
while (length)
{
printf("%08X ", offset );
for (int i=0; i<16; i++)
{
if (i == 8)
printf(" ");
if (length)
{
BYTE b = *p_temp++;
printf("%02X ", b);
length--;
s[i] = GetVisibleChar(b);
}
else//length==0
{
printf(" ");
s[i] = ' ';
}
offset++;
}
printf(" %s/r/n", s);
}
}
void main()
{
CRead read_obj("read.exe");
read_obj.PrintToScreen();
}