Skip to content

Instantly share code, notes, and snippets.

@vladdu
Last active August 29, 2015 14:14
Show Gist options
  • Save vladdu/0142b568a72430fcccf0 to your computer and use it in GitHub Desktop.
Save vladdu/0142b568a72430fcccf0 to your computer and use it in GitHub Desktop.
Dump binary as hex
dump_hex(Binary) ->
dump_hex(Binary, 32).
dump_hex(Binary, Size) ->
dump_hex(Binary, Size, []).
dump_hex(Bin, Size, R) ->
case Bin of
<<H:Size/binary, T/binary>> ->
dump_hex(T, Size, [ dump_hex_line(binary_to_list(H), Size) | R]);
_ ->
lists:flatten(lists:reverse([dump_hex_line(binary_to_list(Bin), Size) | R]))
end.
dump_hex_line(L, Size) when is_list(L), is_integer(Size) ->
P1 = [io_lib:format("~2.16.0B~c", [X, mark(X)]) || X<-L],
La = [ case X<32 of true->$.; false->X end || X<-L],
P2 = [io_lib:format("~c", [X]) || X<-La],
lists:flatten([io_lib:format("~-*s", [Size*3, lists:flatten(P1)]), " |", P2, "|"]) ++ [$\n].
mark(C) when C>127 -> $!;
mark(_) -> $\s.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment