Module: TrickBag::BinaryToHexAndAscii
- Defined in:
- lib/trick_bag/formatters/binary_to_hex_and_ascii.rb
Overview
0x 0 00 07 0E 15 | 1C 23 2A 31 | 38 3F 46 4D | 54 5B 62 69 .….#*18?FMT[bi 0x 10 70 77 7E 85 | 8C 93 9A A1 | A8 AF B6 BD | C4 CB D2 D9 pw~.….….….
Class Method Summary collapse
- .ascii_char(byte) ⇒ Object
- .ascii_string(bytes) ⇒ Object
- .bytes_as_hex(bytes) ⇒ Object
- .format(byte_array_or_string) ⇒ Object
- .format_line(offset, bytes) ⇒ Object
- .join_hex_sections(sections) ⇒ Object
- .offset_string(offset) ⇒ Object
Class Method Details
.ascii_char(byte) ⇒ Object
52 53 54 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 52 def ascii_char(byte) (32..126).include?(byte) ? byte.chr : '.' end |
.ascii_string(bytes) ⇒ Object
57 58 59 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 57 def ascii_string(bytes) bytes.map { |b| ascii_char(b) }.join end |
.bytes_as_hex(bytes) ⇒ Object
37 38 39 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 37 def bytes_as_hex(bytes) bytes.map { |b| "%02X" % b }.join(' ') end |
.format(byte_array_or_string) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 20 def format(byte_array_or_string) byte_array = if byte_array_or_string.is_a?(String) byte_array_or_string.bytes else byte_array_or_string end result = '' offset = 0 byte_array.each_slice(16) do |bytes| result << format_line(offset, bytes) << "\n" offset += 16 end result end |
.format_line(offset, bytes) ⇒ Object
62 63 64 65 66 67 68 69 70 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 62 def format_line(offset, bytes) sections = bytes.each_slice(4).to_a.map { |slice| bytes_as_hex(slice) } offset_string(offset) \ << ' ' \ << join_hex_sections(sections).ljust(53) \ << ' ' \ << ascii_string(bytes) end |
.join_hex_sections(sections) ⇒ Object
47 48 49 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 47 def join_hex_sections(sections) sections.join(' | ') end |
.offset_string(offset) ⇒ Object
42 43 44 |
# File 'lib/trick_bag/formatters/binary_to_hex_and_ascii.rb', line 42 def offset_string(offset) "0x%4X" % [offset] # Offset, e.g. "0x 0" or "0x 10" end |