Created
May 10, 2019 21:48
-
-
Save vdinovi/c152b47be0db4e16e80474855b3d3380 to your computer and use it in GitHub Desktop.
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 characters
| #!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment