[erlang-questions] Help! a log dump function from C.

Hynek Vychodil <>
Wed Apr 22 11:57:47 CEST 2009


Here is one which using io_lib extensively:

dump_hex2(L) -> dump_hex2(0, L, []).

dump_hex2(_, [], R) -> lists:reverse(R);
dump_hex2(N,
          [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13,
           B14, B15, B16
           | T],
          R) ->
    Line = dump_line(N,
                     [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
B13,
                      B14, B15, B16]),
    dump_hex2(N + 16, T, [Line | R]);
dump_hex2(N, L, R) ->
    Line = dump_line(N, L), dump_hex2(N, [], [Line | R]).

dump_line(N, L) ->
    io_lib:format("~8.16.0b:~-48s - ~s~n",
                  [N, [io_lib:format(" ~2.16.0b", [X]) || X <- L],
                   [if X < $\s; X > 126 -> $.;
                       true -> X
                    end
                    || X <- L]]).

On Wed, Apr 22, 2009 at 2:03 AM, feiman <> wrote:

> hi,All
>   I have a function from C,which print out detail hex information
> about a buffer,who can help me "translate" it into Erlang?
>
> when I run
>        const char *teststr = "teststr1234567890abcd";
>
>        dump_hex((const unsigned char *)teststr,strlen(teststr));
>
> it print out
>
> 00000000: 74 65 73 74 73 74 72 31 32 33 34 35 36 37 38 39 -
> teststr123456789
> 00000010: 30 61 62 63 64                                  - 0abcd
>
> thanks all in advance
>
> 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 */
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://www.erlang.org/mailman/listinfo/erlang-questions
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill your
boss.  Be a data hero!
Try Good Data now for free: www.gooddata.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20090422/d7824bcf/attachment.html>


More information about the erlang-questions mailing list