티스토리 뷰

Programming/C

[C++] c++ hexdump

Tribal 2019. 2. 1. 13:31

  개발하면서 디버깅 바로 하려 하다보니 hexdump를 자주 쓰게 되는 것 같다.. 직접 다 만든 것은 아니고, stack overflow에 paddy라는 분이 구현해 두었던 걸 함수 형태로 쓰기 쉽게 바꿔둔게 끝이다.


코드 (원본 : https://stackoverflow.com/questions/16804251/how-would-i-create-a-hex-dump-utility-in-c)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <iomanip>
 
#include <Windows.h>
 
void
hexdump(
    __in BYTE * p,
    __in DWORD len)
{
    DWORD address = 0;
    DWORD row = 0;
    DWORD nread = 0;
 
    std::cout << std::hex << std::setfill('0');
    while (1) {
        // Show the address
        std::cout << std::setw(8<< address;
 
        if (address >= len) break;
        nread = ((len - address) > 16) ? 16 : (len - address);
 
        // Show the hex codes
        for (DWORD i = 0; i < 16; i++)
        {
            if (i % 8 == 0std::cout << ' ';
            if (i < nread)
                    std::cout << ' ' << std::setw(2<< (DWORD)p[16 * row + i];
            else
                std::cout << "   ";
        }
 
        // Show printable characters
        std::cout << "  ";
        for (DWORD i = 0; i < nread; i++)
        {
            if (p[16 * row + i] < 32std::cout << '.';
            else std::cout << p[16 * row + i];
        }
 
        std::cout << "\n";
        address += 16;
        row++;
    }
    std::cout << std::dec;
}
 
int 
main(
    __in int argc,
    __in char* argv[]) 
{
    BYTE buf[0x100= {0,};
    DWORD dwLen = 0x100;
 
    hexdump(buf, dwLen);
}
cs



참고


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31