c 十六进制打印

来源:互联网 发布:java工程师招聘 编辑:程序博客网 时间:2024/06/18 13:55
void dump_hex( const unsigned char *buf, int len)
{
        int i;
        int nlocal;
        const unsigned char *pc;
        char *out;
        const unsigned char *start;
        char c;
        char line[100];

        start = buf;

        while (len > 0)
        {
                sprintf(line, "%08x: ", buf - start);
                out = line + 10;

                for (i = 0, pc = buf, nlocal = len; i < 16; i++, pc++)
                {
                        if (nlocal > 0)
                        {
                                c = *pc;

                                *out++ = NIBBLE((c >> 4) & 0xF);
                                *out++ = NIBBLE(c & 0xF);

                                nlocal--;
                        }
                        else
                        {
                                *out++ = ' ';
                                *out++ = ' ';
                        }                       /* end else */

                        *out++ = ' ';
                }                       /* end for */

                *out++ = '-';
                *out++ = ' ';

                for (i = 0, pc = buf, nlocal = len;
                        (i < 16) && (nlocal > 0);
                        i++, pc++, nlocal--)
                {
                        c = *pc;

                        if ((c < ' ') || (c >= 126))
                        {
                                c = '.';
                        }

                        *out++ = c;
                }                       /* end for */

                *out++ = 0;

                llog(L_NOTICE,"%s", line);

                buf += 16;
                len -= 16;
        }                               /* end while */
}                               /* end dump */

原创粉丝点击