读一个文件,将格式如十六进制编辑器(如WinHex) 样式显示出来

 读一个文件,格式如十六进制编辑器

 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();
   
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值