<br><br><div class="gmail_quote">On Wed, Apr 22, 2009 at 9:27 AM, Hynek Vychodil <span dir="ltr"><<a href="mailto:vychodil.hynek@gmail.com">vychodil.hynek@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
There are many many approaches to do this, one that doesn't use io or io_lib follows:<br><br>-module(dump_hex).<br><br>-compile(inline).<br><br>-export([dump_hex/1]).<br><br>dump_hex(L) -> lists:reverse(dump_hex(0, L, [])).<br>
<br>dump_hex(_, [pad | _], R) -> R;<br>dump_hex(_, [], R) -> R;<br>dump_hex(N,<br> [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12, B13,<br> B14, B15, B16<br> | T],<br> R) -><br> dump_hex(N + 16, T,<br>
[[addr(N), $:,<br> [[$\s | hex(X)]<br> || X<br> <- [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,<br> B13, B14, B15, B16]],<br> " - ",<br>
[print(X)<br>
|| X<br> <- [B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,<br> B13, B14, B15, B16]],<br> $\n]<br> | R]);<br>dump_hex(N, L, R) -><br> dump_hex(N,<br> L ++<br>
[pad, pad, pad, pad, pad, pad, pad, pad, pad, pad, pad,<br> pad, pad, pad, pad, pad],<br> R).<br><br>addr(X) -><br> [nibble(X bsr 28), nibble(X bsr 24), nibble(X bsr 20),<br> nibble(X bsr 16), nibble(X bsr 12), nibble(X bsr 8),<br>
nibble(X bsr 4), nibble(X)].<br><br>hex(pad) -> " ";<br>hex(X) -> [nibble(X bsr 4), nibble(X)].<br><br>nibble(X) -> nibble_(X band 15).<br><br>nibble_(X) when X < 10 -> X + $0;<br>nibble_(X) -> X + $a.</blockquote>
<div><br>There is error, It should be<br><br> nibble_(X) -> X + $a - 10.</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
<br>print(pad) -> [];<br>print(X) when X < $\s, X > 126 -> $.;<br>print(X) -> X.<br><br>Usage:<br><br>1> c(dump_hex).<br>{ok,dump_hex}<br>2> io:put_chars(dump_hex:dump_hex("teststr1234567890abcd")).<div class="im">
<br>
00000000: 74 65 73 74 73 74 72 31 32 33 34 35 36 37 38 39 - teststr123456789<br>00000010: 30 61 62 63 64 - 0abcd<br></div>ok<div><div></div><div class="h5"><br><br><br><div class="gmail_quote">
On Wed, Apr 22, 2009 at 2:03 AM, feiman <span dir="ltr"><<a href="mailto:lfeiman888@gmail.com" target="_blank">lfeiman888@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">hi,All<br>
I have a function from C,which print out detail hex information<br>
about a buffer,who can help me "translate" it into Erlang?<br>
<br>
when I run<br>
const char *teststr = "teststr1234567890abcd";<br>
<br>
dump_hex((const unsigned char *)teststr,strlen(teststr));<br>
<br>
it print out<br>
<br>
00000000: 74 65 73 74 73 74 72 31 32 33 34 35 36 37 38 39 -<br>
teststr123456789<br>
00000010: 30 61 62 63 64 - 0abcd<br>
<br>
thanks all in advance<br>
<br>
void dump_hex( const unsigned char *buf, int len)<br>
{<br>
int i;<br>
int nlocal;<br>
const unsigned char *pc;<br>
char *out;<br>
const unsigned char *start;<br>
char c;<br>
char line[100];<br>
<br>
start = buf;<br>
<br>
while (len > 0)<br>
{<br>
sprintf(line, "%08x: ", buf - start);<br>
out = line + 10;<br>
<br>
for (i = 0, pc = buf, nlocal = len; i < 16; i++, pc++)<br>
{<br>
if (nlocal > 0)<br>
{<br>
c = *pc;<br>
<br>
*out++ = NIBBLE((c >> 4) & 0xF);<br>
*out++ = NIBBLE(c & 0xF);<br>
<br>
nlocal--;<br>
}<br>
else<br>
{<br>
*out++ = ' ';<br>
*out++ = ' ';<br>
} /* end else */<br>
<br>
*out++ = ' ';<br>
} /* end for */<br>
<br>
*out++ = '-';<br>
*out++ = ' ';<br>
<br>
for (i = 0, pc = buf, nlocal = len;<br>
(i < 16) && (nlocal > 0);<br>
i++, pc++, nlocal--)<br>
{<br>
c = *pc;<br>
<br>
if ((c < ' ') || (c >= 126))<br>
{<br>
c = '.';<br>
}<br>
<br>
*out++ = c;<br>
} /* end for */<br>
<br>
*out++ = 0;<br>
<br>
llog(L_NOTICE,"%s", line);<br>
<br>
buf += 16;<br>
len -= 16;<br>
} /* end while */<br>
} /* end dump */<br>
<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://www.erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://www.erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><br></div></div><font color="#888888">-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss. Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com" target="_blank">www.gooddata.com</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss. Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>