Last active
August 29, 2015 14:14
-
-
Save vladdu/0142b568a72430fcccf0 to your computer and use it in GitHub Desktop.
Dump binary as hex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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