티스토리 뷰

Programming/C

dumpcode.h

Tribal 2016. 7. 5. 16:46
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
void printchar(unsigned char c)
{
        if(isprint(c))
                printf("%c",c);
        else
                printf(".");
}
void dumpcode(unsigned char *buff, int len)
{
        int i;
 
        for(i=0; i<len; i++)
        {
                if(i%16==0)
                        printf("0x%08x  ",&buff[i]);
                printf("%02x ",buff[i]);
                if(i%16-15==0)
                {
                        int j;
                        printf("  ");
                        for(j=i-15;j<=i;j++)
                                printchar(buff[j]);
                        printf("\n");
                }
        }
        if(i%16!=0)
        {
                int j;
                int spaces=(len-i+16-i%16)*3+2;
 
                for(j=0;j<spaces;j++)
                        printf(" ");
                for(j=i-i%16;j<len;j++)
                        printchar(buff[j]);
        }
        printf("\n");
}
cs


/usr/include 에 저장하면 편하게 쓸 수 있음


헤더 호출 후 사용방법 :

1
2
3
4
5
6
7
8
9
10
11
#include <dumpcode.h> // /usr/include에 저장한 경우..
#include "./dumpcode.h" // 그냥 저장한 경우
 
int main(void)
{
    char buffer[128];
    int i = 0;
 
    dumpcode(buffer, 128);
    dumpcode(&i, 4);
}
cs


댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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