Number Formatting Question

Brent Fulgham bfulg@REDACTED
Fri Feb 13 00:25:11 CET 2004


I'm a newcoming to Erlang, so please bear with me.

Is there any way to instruct Erlang to format a number
with leading zeros?

For example, I wanted to produce a standard
hexadecimal dump of some IPTrace data.  I figured out
how to format the correct spacing (see the following
pathetic Erlang hack):

%  Routine to pretty-print datagrams
print_raw_bytes([], Count) ->
    io:format("~n");
print_raw_bytes([H|Tail], Count) ->
    io:format("~2.16B", [H]),
    Z= Count rem 2,
    IncCount = case Count of
        1 -> Count + 1;
	8 -> io:format("  "), Count + 1;
        16 -> io:format("~n"), 1;
	_ ->
	    if
	    	Z == 0

This produces the desired output as follows:
RAW SEND DATA:
 0 C  0 0  0 1  015  6D74 735F 696E 7172
 0 B  230 3030 3031  3233 3535  0
RAW RECEIVED PACKET:
 0 B  0 0  0 1  01A   0 0  016  035 3230
32 0  0 0  0 0  0 0   0 2  0 0 1387  0 0
 0 0 

What I am trying to achieve is:
RAW SEND DATA:
000C 0000 0001 0015  6D74 735F 696E 7172
000B 0230 3030 3031  3233 3535 00
RAW RECEIVED PACKET:
000B 0000 0001 001A  0000 0016 0035 3230
3200 0000 0000 0000  0002 0000 1387 0000
0000 

Any ideas?

Thanks,

-Brent



More information about the erlang-questions mailing list