[erlang-questions] Formatting question

Burkhard Neppert bneppert@REDACTED
Sat Aug 4 21:06:23 CEST 2007


I've two question about string handling

I'd like to convert binary data to its hex string representation.
E.g. bin2hex(<<255:16>>) ->  "00FF"
The (buggy) solution I've come with so far is

bin2hex(X) ->
    lists:foldr(fun (Byte, R) -> [hd(io_lib:fwrite("~.16B", [Byte]))|R] 
end, [], binary_to_list(X))e.

First thing to note is, that it does not do the things it should :-(
Reason is that io_lib:fwrite does not insert a leading zero if Byte is 
less than F.

Question no. 1: how do I specify pad characters with  ~B formatting option ?

Question no. 2: the solution somehow looks ugly. That cannot be right.
Does anyone have a more succinct solution for bin2hex ?



More information about the erlang-questions mailing list