Starting slowly and questions...

Magnus Thoäng magnus.thoang@REDACTED
Fri Jul 2 16:25:34 CEST 2004


Rob wrote:
...
> Digest = erlang:md5(Contents).
> 
> io:format("~.16b\n", [Digest]).
> 
...
> I'd like to get it to output something like:
> f28c94b1
> 
> maybe there is a function to turn a arbitrary length binary into a hex
> string but
> 

Hi Rob,
As far as I know, there is no such function in OTP.

The b control sequence is for integers (not binaries), so if you need to 
dump out reasonably short binaries, you might do something like...

MyBinary = some_function(SomeArguments,...),
MyBinaryBitSize = 8 * size(MyBinary),
<<MyInteger:MyBinaryBitSize/integer>> = MyBinary,
io:format("~*.16.0b\n", [size(MyBinary) * 2, MyInteger]).

...but something like the following is much more erlangish...

dump_binary(IODevice, <<>>) ->
   io:format(IODevice, "~n", []);

dump_binary(IODevice, <<Octet, Rest/binary>>) ->
   io:format(IODevice, "~2.16.0b\n", [Octet]),
   dump_binary(IODevice, Rest).

...and in the case you want to group the bytes in some way, it is easy 
to accomplish with such a function.

-- 
Magnus



More information about the erlang-questions mailing list