ExBin v0.4.0 ExBin.Formatter View Source

Link to this section Summary

Functions

Formats a bitstring into a string of ones and zeroes

Pretty print a given binary. By default this will output in the same format as hexdump -C <file> from BSD general commands. This function returns a string, so you aren't bound to any certain I/O mechanism (i.e. you may not want to print this to stdout, so IO.puts/1 is not used)

Link to this section Functions

Link to this function

format_bits(bitstr, opts \\ []) View Source

Formats a bitstring into a string of ones and zeroes.

opts allows you to optionally set some style settings. Currently, only one is supported:

  • :spacer - if true, this will insert a space between each set of 8 bits; defaults to false.

Examples

iex> ExBin.Formatter.format_bits(<<0b1011011100010101::16>>)
"1011011100010101"

iex> ExBin.Formatter.format_bits(<<0b1011011100010101::16>>, spacer: true)
"10110111 00010101"
Link to this function

format_bytes(binary, opts \\ []) View Source

Pretty print a given binary. By default this will output in the same format as hexdump -C <file> from BSD general commands. This function returns a string, so you aren't bound to any certain I/O mechanism (i.e. you may not want to print this to stdout, so IO.puts/1 is not used).

The opts keyword list argument can contain the following options:

  • :canonical - either true or false; whether output should be in canonical format or not (-C flag to hexdump BSD command). Defaults to true.
  • :start - an integer value to start indexing at when outputting; defaults to zero.

Examples

iex> ExBin.Formatter.format_bytes(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20>>, canonical: false)
"0000000 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f\n0000010 1011 1213 14"

iex> ExBin.Formatter.format_bytes(<<0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20>>, start: 0xa0, canonical: false)
"00000a0 0001 0203 0405 0607 0809 0a0b 0c0d 0e0f\n00000b0 1011 1213 14"

Canonical format

The :canonical option, which is the default, will produce an output as if you were running

hexdump -C <file>
iex> ExBin.Formatter.format_bytes("Foo:bar\nfizz\tbang" <> <<0xf9, 0xd4>>)
"00000000  46 6f 6f 3a 62 61 72 0a  66 69 7a 7a 09 62 61 6e  |Foo:bar.fizz.ban|\n00000010  67 f9 d4                                          |g..|"