class StringLineHexdumper

Dump a line of text as hex dump.

Author

Peter Kofler

Constants

NIBBLE_FORMAT_STR
NIBBLE_SIZE

Each displayed number is 2 nibbles, i.e. it's a byte.

NIBBLE_WHITE_SPACE

Public Class Methods

empty(columns) click to toggle source

Factory method to create a formatter for an empty line with columns length.

# File lib/javaclass/string_hexdump.rb, line 49
def self.empty(columns)
  StringLineHexdumper.new(0, columns, '')
end
new(address, columns, data) click to toggle source
# File lib/javaclass/string_hexdump.rb, line 53
def initialize(address, columns, data)
  @address = address
  @maxlen = columns
  @data = data
end

Public Instance Methods

format() click to toggle source
# File lib/javaclass/string_hexdump.rb, line 59
def format
  address = format_address
  hexbytes = format_bytes
  space = add_whitespace
  display = strip_non_printable
  
  "#{address}: #{hexbytes.join}#{space}; #{display}\n"
end

Private Instance Methods

add_whitespace() click to toggle source
# File lib/javaclass/string_hexdump.rb, line 84
def add_whitespace
  NIBBLE_WHITE_SPACE * (@maxlen - @data.size)
end
format_address() click to toggle source

Format the address to a 8 digit hex number.

# File lib/javaclass/string_hexdump.rb, line 71
def format_address
  sprintf('%8.8Xh', @address)
end
format_byte(byte) click to toggle source

Format the bytes value to a NIBBLE_SIZE digit hex number.

# File lib/javaclass/string_hexdump.rb, line 80
def format_byte(byte)
  sprintf(NIBBLE_FORMAT_STR, byte)
end
format_bytes() click to toggle source
# File lib/javaclass/string_hexdump.rb, line 75
def format_bytes
  @data.unpack('C' * @data.size).collect{ |c| format_byte(c) }
end
strip_non_printable() click to toggle source
# File lib/javaclass/string_hexdump.rb, line 88
def strip_non_printable
  @data.strip_non_printable
end