查看文件的十六进制VHEX

查看文件的十六进制在WINDOWS下确实很不方便,于是自己动手写个小程序查看

对于小文件是可以应付的

链接:https://pan.baidu.com/s/1KQxB-6eMKa90wmg_QfogJA 
提取码:vhex 
 

#include "debug.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <windows.h>
#include <unistd.h>
#define D9(XX,...)  do{  printf(XX,##__VA_ARGS__);  }while(0)
#include "yfile.h"
#include <string>
using namespace std;

struct HEX_INFO
{
    uint8_t  hex_items;
    uint8_t  hex_lines;
    string filename;
    char *buf;
    uint32_t begin;
    uint32_t base;
    uint32_t filesize;
    uint32_t view;
    bool last;
    HEX_INFO()
    {
        base = 0;
        filesize = 0;
        view = 0;
        hex_items = 16;
        hex_lines = 20;
        begin = -1;
        last = false;
    }
} info;
void man(const char *app)
{
    D9("vhex,to view hex in the right way!(http://blog.csdn.net/wisepragma)\n");
    D9("usage: vhex [-l][-b base_addr][-i start_addr][-r hex items][-n lines]\n");
    D9("parameter error!\n");
    D9("man:\n\%s  file\n", app);
    D9("type '<enter> to show all\n");
    D9("type ]<enter> to show last\n");    
}
//#include <unistd.h>
//#include <getopt.h>
int main(int argc, char *argv[])
{
    while(optind < argc)
    {
        switch(getopt(argc, argv, "lLb:B:i:I:R:r:N:n:"))
        {
            case EOF:
                // // D9("optind:%d:%s\n",optind,argv[optind]);
                info.filename = argv[optind];
                printf("FILE:%d:%s\n", optind, info.filename.c_str());
                optind++;  // Skip to the next argument
                break;
            case 'l':
            case 'L':
            {
                info.last = true;
                D9("SHOW LAST FEW LINES\n");
            }
            break;
            case 'r':
            case 'R':
            {
                info.hex_items = atoi(optarg);
                D9("ITEMS:0x%X,%d\n", info.hex_items, info.hex_items);
            }
            break;
            case 'i':
            case 'I':
            {
                info.begin = strtoul(optarg, 0, 0);
                D9("BEGIN:0x%X,%d\n", info.begin, info.begin);
            }
            break;
            case 'b':
            case 'B':
            {
                info.base = strtoul(optarg, 0, 0);
                D9("BASE:0x%X,%d\n", info.base, info.base);
            }
            break;
            case 'N':
            case 'n':
            {
                //info.hex_lines = atoi( optarg );
                info.hex_lines = strtoul(optarg, 0, 0);
                D9("LINES:0x%X,%d\n", info.hex_lines, info.hex_lines);
            }
            break;
            case 'h':
            case 'H':
            case '?':
            {
                man(argv[0]);
                return 0;
                //RETURN( ERR_OK );
            }
            break;
        }
    }
    YFILE f;
    if(argc >= 2 && f.set(info.filename.c_str()))
    {
        uint8_t *pbuf = f.buf();
        uint32_t fsize = f.size();
        uint32_t idx;
        if(info.begin == (uint32_t) -1)
        {
            info.begin = info.base;
            idx = 0;
        }
        else
        {
            idx = info.begin - info.base;
        }
        if(info.last)
        {
            idx = fsize - info.hex_items * info.hex_lines + 10;
            D9("\n\nlast few lines:\n");
        }
        char show[ info.hex_items + 1] = {0};
        bool b_showall = false;
        while(idx < fsize)
        {
            if(idx == 0 || (idx /  info.hex_items %  info.hex_lines) == 0)
            {
                D9("\n@ADDRESS  ");
                for(int i = 0; i < info.hex_items ; i++)
                {
                    if(i < 0x10) { D9("+"); }
                    D9("%X ", i);
                    if((i + 1) % 4 == 0) { D9(" "); }
                }
                D9("\n");
                for(int i = 0; i < info.hex_items / 4 + info.hex_items * 3 + info.hex_items; i++)
                {
                    D9("=");
                }
                D9("==============\n");
            }
            memset(show, 0, sizeof(show));
            D9("%08X  ", idx + info.base);
            for(int i = 0; i < info.hex_items ; i++)
            {
                if(idx < fsize)
                {
                    show[i] = (pbuf[idx] >= 32 && pbuf[idx] <= 127) ? pbuf[idx] : '.';
                    D9("%02X ", pbuf[idx++]);
                }
                else
                {
                    show[i] = ' ';
                    D9("   ");
                }
                if((i + 1) % 4 == 0) { D9(" "); }
            }
            D9("    %s\n", show);
            if(!b_showall && (idx /  info.hex_items %  info.hex_lines) == 0)
            {
                char ch = getchar();
                if('\'' == ch)
                {
                    b_showall = true;
                }
                if(']' == ch)
                {
                    idx = fsize - info.hex_items * info.hex_lines + 10;
                    D9("\n\nlast few lines:\n");
                }
            }
        }
    }
}

 
#include <tchar.h>
#include <stdio.h>
#include <locale.h>
#include "debug.h"
#include <string>
#include <windows.h>
#pragma once
using namespace std;
class YFILE
{
private:
    string m_file_name;
    size_t m_file_size;
    size_t m_buf_size;
    size_t m_real_size;
    uint8_t *m_buf;
    HANDLE m_hfile;
    void init()
    {
        m_hfile = INVALID_HANDLE_VALUE;
        m_buf_size = 0;
        m_real_size  = 0;
        m_file_size = 0;
        m_buf = nullptr;
    }

public:
    YFILE()
    {
        init();
    }

    YFILE(const char *file_name)
    {
        init();
        set(file_name);
    }

    bool set(const char *filename)
    {
        m_file_name = filename;
        m_hfile = CreateFile(m_file_name.c_str(),    // \\?\  INVALID_HANDLE_VALUE
                             GENERIC_READ,
                             // ( mode == MODE_READONLY ) ? GENERIC_READ : ( GENERIC_READ | GENERIC_WRITE ), //读,写,0(读属性信息)
                             // FILE_SHARE_WRITE | FILE_SHARE_READ, //FILE_SHARE_DELETE
                             FILE_SHARE_READ | FILE_SHARE_WRITE,
                             NULL,               // default security
                             OPEN_EXISTING,//  ( mode == MODE_REPLACE || mode == MODE_NEW ) ? CREATE_ALWAYS : OPEN_EXISTING,
                             //CREATE_NEW  (若文件不存在,则创建成功,返回HANDLE,若文件存在,则创建失败,返回INVALID_HANDLE_VALUE)
                             //CREATE_ALWAYS  (文件存在就变成空,不存在则始终创建)--覆盖式保存 MODE_REPLACE
                             //OPEN_ALWAYS   文件不存在就创建成空的文件,存在就打开
                             //OPEN_EXISTING  存在打开成功,不存在打开失败--选择式打开的文件
                             //TRUNCATE_EXISTING  存在则文件变为空, 不存在则失败
                             FILE_ATTRIBUTE_NORMAL, // FILE_ATTRIBUTE_READONLY,FILE_FLAG_DELETE_ON_CLOSE,FILE_FLAG_OVERLAPPED,FILE_FLAG_RANDOM_ACCESS
                             NULL);                 // no attr. template
        if(m_hfile != INVALID_HANDLE_VALUE)
        {
            ULARGE_INTEGER liSize;
            m_file_size = (size_t) GetFileSizeEx(m_hfile, (LARGE_INTEGER *)&liSize) ? (liSize.QuadPart) : (-1);       //GetFileSizeEx() succeed return nonzero,otherwise zero
            if(m_file_size != 0 && m_file_size != (size_t) -1)
            {
                //BYTE *m_buf = NULL;
                if(NULL != m_buf)
                {
                    delete[] m_buf;
                    m_buf = 0;
                }
                m_buf_size = m_file_size + 1;
                m_buf = new BYTE[(unsigned int)m_buf_size];   //warning C4244: “初始化”: 从“LONGLONG”转换到“unsigned int”,可能丢失数据
                // memset (&m_buf,0,  (unsigned int)m_file_size);//加入会产生错误 why??
                m_buf[m_file_size] = 0;
                if(NULL != m_buf)
                {
                    SetFilePointer(m_hfile, 0, 0, FILE_BEGIN);
                    DWORD  NumberOfBytesRead = 0;
                    ReadFile(m_hfile, m_buf, (DWORD)m_file_size/*bytes need*/, &NumberOfBytesRead/*ret bytes have read*/, NULL);     //从hLoadedFile读取liDistance.LowPart个字节到m_pbFileBuffer中,并在NumberOfBytesRead中返回实际读取字节数
                    m_real_size = NumberOfBytesRead;
                    return true;
                }
            }
        }
        return false;
    }

    uint8_t *buf(void)
    {
        return m_buf;
    }
    size_t size(void)
    {
        return m_real_size;
    }



    uint8_t operator[](unsigned int index)
    {
        if(index < size())
        {
            return m_buf[index];
        }
        else
        {
            return 0;
        }
    }
    ~YFILE()
    {
        clean();
    }
    void clean()
    {
        if(m_buf)
        {
            delete[] m_buf;
            m_buf = 0;
        }
        if(m_hfile)
        {
            CloseHandle(m_hfile);
            m_hfile = NULL;
        }
    }
};


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值