Class: HexHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/hexhelper.rb

Overview

hexhelper.rb Created November, 2012 By Ron Bowes

See: LICENSE.md

This is a simple utility class that converts an arbitrary binary string into a user-readable string.

Constant Summary collapse

BYTE_NUMBER_LENGTH =
8
SPACES_BEFORE_HEX =
2
SPACES_BEFORE_ASCII =
2
LINE_LENGTH =
16

Class Method Summary collapse

Class Method Details

.to_s(data, indent: 0, offset: nil) ⇒ Object

Convert the arbitrary binary data, ‘data’, into a user-readable string.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hexhelper.rb', line 20

def self.to_s(data, indent: 0, offset: nil)
  length = data.length
  out = (' ' * indent)

  0.upto(length - 1) do |i|
    # This section encodes all the hex lines, including the final partial line
    if((i % LINE_LENGTH) == 0)
      if(i != 0)
        # Subtract one from the indent because we print a space surrounding
        # the actual hex number
        out = out + "\n" + (' ' * indent)
      end
      # Minus 1, so we can potentially replace the last one with a current-
      # place marker
      out = out + ("%0#{BYTE_NUMBER_LENGTH}x" % i) + " " * (SPACES_BEFORE_HEX - 1)
      if(offset && (i == offset))
        out = out + '<'
      else
        out = out + ' '
      end
    end

    out = out + ("%02x" % data[i].ord)
    # Always put a '>' immediately after the highlighted offset
    if(i == offset)
      out = out + '>'
    # Put a '>' after the value before the chosen offset, unless we're at
    # the end of a line or the absolute end
    elsif((i + 1 == offset) && (((i + 1) % LINE_LENGTH) != 0) && ((i + 1) % length) != 0)
      out = out + '<'
    else
      out = out + ' '
    end

    if(((i + 1) % LINE_LENGTH) == 0)
      out = out + (" " * SPACES_BEFORE_ASCII)
      LINE_LENGTH.step(1, -1) do |j|
        out = out + ("%c" % ((data[i + 1 - j].ord > 0x20 && data[i + 1 - j].ord < 0x80) ? data[i + 1 - j].ord : ?.))
      end
    end

  end

  # This encodes the partial line of ascii, plus the spaces before
  if((length % LINE_LENGTH) != 0)
    # Add extra spaces between the hex and the ascii
    (length % LINE_LENGTH).upto(LINE_LENGTH - 1) do |i|
      out = out + ('   ') # The width of a hex character and a space
    end
    out = out + (' ' * SPACES_BEFORE_ASCII)

    # Add the final line of characters
    (length - (length % LINE_LENGTH)).upto(length - 1) do |i|
      out = out + ("%c" % ((data[i].ord > 0x20 && data[i].ord < 0x80) ? data[i].ord : ?.))
    end
  end

  return out
end