LingYun Studio embeded system framwork software, such as thirdparty build shell and lingyun library
guowenxue
2024-08-21 7deaa4a49b1d0fb2112a69719141709b9c261c7c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue <guowenxue@gmail.com>
 *                  All rights reserved.
 *
 *       Filename:  dump.c
 *    Description:  This is the buffer dump print code.
 *
 *        Version:  1.0.0(08/08/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "08/08/2012 06:51:40 PM"
 *
 ********************************************************************************/
 
#include <stdio.h>
 
void dump_buf(const char *prompt, const unsigned char *buf, size_t len)
{
    char        line[256];
    size_t      i, j;
    int         offset;
 
    if( prompt )
    {
        printf("%s", prompt);
    }
 
    for(i = 0; i < len; i += 16)
    {
        offset = snprintf(line, sizeof(line), "%08zx: ", i);
 
        /* Print hex representation */
        for (j = 0; j < 16; j++)
        {
            if (i + j < len)
                offset += snprintf(line + offset, sizeof(line) - offset, "%02x ", buf[i + j]);
            else
                offset += snprintf(line + offset, sizeof(line) - offset, "   ");
        }
 
        offset += snprintf(line + offset, sizeof(line) - offset, " ");
 
        /* Print ASCII representation */
        for (j = 0; j < 16; j++)
        {
            if (i + j < len)
            {
                unsigned char c = buf[i + j];
                offset += snprintf(line + offset, sizeof(line) - offset, "%c", (c >= 32 && c <= 126) ? c : '.');
            }
            else
            {
                offset += snprintf(line + offset, sizeof(line) - offset, " ");
            }
        }
 
        /* Print the line */
        printf("%s\n", line);
    }
}
 
int main (int argc, char **argv)
{
    char    buf[256];
    int     i;
 
    for(i=0; i<sizeof(buf); i++)
        buf[i] = i;
 
    dump_buf("Hex dump buffer content:\n", buf, sizeof(buf));
 
    return 0;
}