module Hex

whoops! 48: d2 b1 6d 31 3e 67 e1 88 99 8b 4b 34 1d 61 05 15 |..m1g.…K4.a..|

Constants

ASCII_PRINTABLE
DUMP_COLORS

Public Class Methods

dump(data, options={}) click to toggle source
# File lib/epitools/hexdump.rb, line 29
def self.dump(data, options={})
  base_offset   = options[:base_offset] || 0
  color         = options[:color].nil? ? true : options[:color]
  highlight     = options[:highlight]

  # p options
  # p color

  lines               = data.scan(/.{1,16}/m)
  max_offset          = (base_offset + data.size) / 16 * 16
  max_offset_width    = max_offset.to_s.size
  max_hex_width       = 3 * 16 + 1

  # p [max_offset, max_offset_width]
  lines.each_with_index do |line,n|
    offset    = base_offset + n*16
    bytes     = line.unpack("C*")
    hex       = bytes.map { |c| "%0.2x" % c }.insert(8, '').join(' ')

    plain = bytes.map do |c|
      if ASCII_PRINTABLE.include?(c)
        c = c.chr
      else
        color ? '<9>.</9>' : '.'
      end
    end.join('')

    puts "<11>#{offset.to_s.ljust(max_offset_width)}<3>:  <14>#{hex.ljust(max_hex_width)} <8>|<15>#{plain}<8>|".colorize(:strip => !color)
  end
end