Method: String#formatted
- Defined in:
- lib/openc3/core_ext/string.rb
#formatted(word_size = 1, words_per_line = 16, word_separator = ' ', indent = 0, show_address = true, address_separator = ': ', show_ascii = true, ascii_separator = ' ', unprintable_character = ' ', line_separator = "\n") ⇒ Object
Displays a String containing binary data in a human readable format by converting each byte to the hex representation.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/openc3/core_ext/string.rb', line 67 def formatted( word_size = 1, words_per_line = 16, word_separator = ' ', indent = 0, show_address = true, address_separator = ': ', show_ascii = true, ascii_separator = ' ', unprintable_character = ' ', line_separator = "\n" ) string = '' byte_offset = 0 bytes_per_line = word_size * words_per_line indent_string = ' ' * indent ascii_line = '' self.each_byte do |byte| if byte_offset % bytes_per_line == 0 # Create the indentation at the beginning of each line string << indent_string # Add the address if requested string << sprintf("%08X%s", byte_offset, address_separator) if show_address end # Add the byte string << sprintf("%02X", byte) # Create the ASCII representation if requested if show_ascii if PRINTABLE_RANGE.include?(byte) ascii_line << [byte].pack('C') else ascii_line << unprintable_character end end # Move to next byte byte_offset += 1 # If we're at the end of the line we output the ascii if requested if byte_offset % bytes_per_line == 0 if show_ascii string << "#{ascii_separator}#{ascii_line}" ascii_line = '' end string << line_separator # If we're at a word junction then output the word_separator elsif (byte_offset % word_size == 0) and byte_offset != self.length string << word_separator end end # We're done printing all the bytes. Now check to see if we ended in the # middle of a line. If so we have to print out the final ASCII if # requested. if byte_offset % bytes_per_line != 0 if show_ascii num_word_separators = ((byte_offset % bytes_per_line) - 1) / word_size existing_length = (num_word_separators * word_separator.length) + ((byte_offset % bytes_per_line) * 2) full_line_length = (bytes_per_line * 2) + ((words_per_line - 1) * word_separator.length) filler = ' ' * (full_line_length - existing_length) ascii_filler = ' ' * (bytes_per_line - ascii_line.length) string << "#{filler}#{ascii_separator}#{ascii_line}#{ascii_filler}" ascii_line = '' end string << line_separator end string end |