Method: String#hexdump
- Defined in:
- lib/pentex/core.rb
#hexdump(opts = {}) ⇒ Object
shows (binary) string in a readable format.
Options:
:bytes => 16; the maximum number of bytes per line
:offset => 0; not supported yet
:max_bytes => 0; the max number of bytes to be processed
:chunksize => 8; splitted by "-"
:range => nil; use this to define the range to be displayes, e.g. "(10..20)" must be a Range-Object!
:format => :simple; this is the only format supported yet
:max_lines => 40; number of lines before stopping output
Example 1:
>> "\x02:this is a string with some binary data\xff".hexdump
000000: 02 3A 74 68 69 73 20 69 - 73 20 61 20 73 74 72 69 :.:this is a stri
000010: 6E 67 20 77 69 74 68 20 - 73 6F 6D 65 20 62 69 6E :ng with some bin
000020: 61 72 79 20 64 61 74 61 - FF :ary data.
=> true
Example 2: giving a range with int and hex value calculation
"\x02:this is a string with some binary data\xff".hexdump :range => (16...0x10+0x10)
000010: 6E 67 20 77 69 74 68 20 - 73 6F 6D 65 20 62 69 :ng with some bi
=> true
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/pentex/core.rb', line 150 def hexdump( opts = {} ) copts = { :bytes => 16, :offset => 0, :max_bytes => 0, :chunksize => 8, :range => nil, :format => :simple, :max_lines => 40 } # lame kind of implementing a command help, anyway better than opening source file ;) unless opts.is_a? Hash puts "[options]" puts copts.to_yaml puts puts "[example]\n>>'this is a string'.hexdump :max_bytes => 8" puts return true end copts.update opts dump = "" offset = 0 linecount = 0 max_bytes = copts[:max_bytes] > 0 ? copts[:max_bytes] : self.length if copts[:range].is_a? Range offset = copts[:range].min max_bytes = copts[:range].max end counter = offset while counter < max_bytes pos = sprintf("%06X", counter) linelen = (counter < max_bytes - copts[:bytes] ) ? copts[:bytes] : (max_bytes - counter) bytes = "" linelen.times do |i| bytes << "%02X" % self[counter + i].ord bytes << " " unless (i+1) % copts[:bytes] == 0 bytes << "- " if ( i+1) % copts[:chunksize] == 0 && (i+1) < linelen end maxlen = ( copts[:bytes] * 2 + copts[:bytes] - 1 + copts[:bytes] / copts[:chunksize] ) bytes << " " * (maxlen - bytes.length) if maxlen > bytes.length ascii = self[counter, linelen].printable line = "#{pos}: #{bytes} :#{ascii}\n" puts line linecount += 1 if linecount >= copts[:max_lines] puts "<Enter>, [qQ]uit" c = gets return false if c =~ /^q/i linecount = 0 end dump << line counter += copts[:bytes] end return dump unless copts[:format] == :simple return true end |