Created
May 10, 2019 21:48
-
-
Save vdinovi/c152b47be0db4e16e80474855b3d3380 to your computer and use it in GitHub Desktop.
Revisions
-
vdinovi created this gist
May 10, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,24 @@ #!/usr/bin/ruby # stupid version of hexdump to just read the first n bytes from stdin # no args supported # useful for when hexdump is not available # # to print out the first two bytes from a file # ./ruby_hexdump 2 < some_file ARGV.count == 1 or raise "Usage: ruby_hexdump <num bytes>" bytes = ARGV[0].to_i buffer = [] $stdin.each_byte do |byte| buffer << ('%02X' % (byte & 0xFF)) break if buffer.count >= bytes end buffer.each_slice(16).each_with_index do |hex_row, index| $stdout.write '%08X ' % (index * 16 % 0xFFFFFFFF) $stdout.write hex_row.join(' ') $stdout.write "\n" end $stdout.flush