[erlang-questions] Help! a log dump function from C.
Hynek Vychodil
vychodil.hynek@REDACTED
Wed Apr 22 09:27:29 CEST 2009
There are many many approaches to do this, one that doesn't use io or
io_lib follows:
-module(dump_hex).
-compile(inline).
-export([dump_hex/1]).
dump_hex(L) -> lists:reverse(dump_hex(0, L, [])).
dump_hex(_, [pad | _], R) -> R;
dump_hex(_, [], R) -> R;
dump_hex(N,
[B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13,
B14, B15, B16
| T],
R) ->
dump_hex(N + 16, T,
[[addr(N), $:,
[[$\s | hex(X)]
|| X
<- [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
B13, B14, B15, B16]],
" - ",
[print(X)
|| X
<- [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
B13, B14, B15, B16]],
$\n]
| R]);
dump_hex(N, L, R) ->
dump_hex(N,
L ++
[pad, pad, pad, pad, pad, pad, pad, pad, pad, pad, pad,
pad, pad, pad, pad, pad],
R).
addr(X) ->
[nibble(X bsr 28), nibble(X bsr 24), nibble(X bsr 20),
nibble(X bsr 16), nibble(X bsr 12), nibble(X bsr 8),
nibble(X bsr 4), nibble(X)].
hex(pad) -> " ";
hex(X) -> [nibble(X bsr 4), nibble(X)].
nibble(X) -> nibble_(X band 15).
nibble_(X) when X < 10 -> X + $0;
nibble_(X) -> X + $a.
print(pad) -> [];
print(X) when X < $\s, X > 126 -> $.;
print(X) -> X.
Usage:
1> c(dump_hex).
{ok,dump_hex}
2> io:put_chars(dump_hex:dump_hex("teststr1234567890abcd")).
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
ok
On Wed, Apr 22, 2009 at 2:03 AM, feiman <lfeiman888@REDACTED> 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
> erlang-questions@REDACTED
> 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/76259f44/attachment.htm>
More information about the erlang-questions
mailing list