Skip to content

Instantly share code, notes, and snippets.

@dkuku
Last active December 9, 2023 10:26
Show Gist options
  • Save dkuku/1d9bbc7b9cf45262f6de8f3635c7b9ec to your computer and use it in GitHub Desktop.
Save dkuku/1d9bbc7b9cf45262f6de8f3635c7b9ec to your computer and use it in GitHub Desktop.
previous_fun = Inspect.Opts.default_inspect_fun()
hexdump = fn
value, opts when not is_binary(value) ->
# change base to hex - this way it will show binary numbers in instead of decimal
previous_fun.(value, %{opts | base: :hex})
value, opts ->
{:ok, string_io} = StringIO.open(value)
printable_range = 0x20..0x7F
column_divider = " "
result =
string_io
|> IO.binstream(2)
|> Stream.take(opts.printable_limit)
|> Stream.chunk_every(8)
|> Stream.map(
&{
# generates the text: AABB CCDD EEFF 1122 3344 5566 7788 9900
Enum.map_join(&1, " ", fn two_chars -> Base.encode16(two_chars) end),
# generates the text: abc...def1234567
for <<char::size(8) <- Enum.join(&1, "")>> do
if Enum.member?(printable_range, char), do: <<char>>, else: "."
end
}
)
|> Stream.with_index()
|> Enum.map_join("\n", fn {{chunk, original_text}, index} ->
[
# generates the first column 00001
String.pad_leading("#{index}", 6, "0"),
# last 0 and divider in the first column
"0",
column_divider,
# empty spaces for the last row when it's not full width
String.pad_trailing(chunk, 40, " "),
column_divider,
original_text
]
end)
StringIO.close(string_io)
"\n" <> result
end
Inspect.Opts.default_inspect_fun(hexdump)
@dkuku
Copy link
Author

dkuku commented Dec 9, 2023

this results in printing binaries with hexdump output:

** (FunctionClauseError) no function clause matching in FitDecoder.DefinitionMessage.parse_architecture/1    
    
    The following arguments were given to FitDecoder.DefinitionMessage.parse_architecture/1:
    
        # 1
        
        0000000  048C 0404 8600 0900 0F00 1600 0004 D225   ...............%
        0000010  0AC6 2840 0001 0003 05FE 0284 0101 0002   ..(@............
        0000020  0102 0301 0204 0284 0000 0001 2FB3 02C6   ............/...
        0000030  4000 0100 1E03 FD04 8600 0284 0102 8441   @..............A
        0000040  0001 0017 03FD 0486 0A02 8407 0486 0025   ...............%
        0000050  0AD4 381D 9C08 B601 250A D438 0180 0000   ..8.....%..8....
        0000060  B046 0025 0AE2 481D B909 CE01 250A E248   .F.%..H.....%..H
        0000070  0180 0000 B066                            .....f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment