Commit 0a9042e4 authored by hydrargyrum's avatar hydrargyrum

vhd: visual hex dump, splitting at newlines, not fixed-width lines

parent 6535e980
......@@ -35,6 +35,7 @@ This repository hosts various small personal tools.
* supybot-shell: Supybot plugin: execute shell commands and see their output
* tailsleep: like tail -f but quits when I/O activity stops
* univisible: tweak Unicode combinations and visualize them
* vhd: visual hex dump, splitting at newlines, not fixed-width lines
* wakeonwan: wake remote machines with Wake-on-WAN
For more info, see also:
#!/usr/bin/env python3
# license: WTFPLv2
# original: 2007
# rewritten for modernization in 2019
from argparse import ArgumentParser, FileType
import sys
'\n': r'\n',
'\t': r'\t',
'\r': r'\r',
'\f': r'\f',
'\b': r'\b',
'\a': r'\a',
'\v': r'\v',
'\x00': r'\0',
' ': 'sp',
def transform(b):
char = chr(b)
if char in REPLACES:
return f' {REPLACES[char]}'
elif 0x20 < b < 0x7f:
return f' {char} '
return f' {b:02x}'
def print_line(line):
print(' '.join(transform(b) for b in line))
def main():
parser = ArgumentParser(description='Visual Hex Dump')
parser.add_argument('--line-addresses', action='store_true')
parser.add_argument('--hex', action='store_true')
parser.add_argument('file', nargs='?', type=FileType('rb'), default=sys.stdin.buffer)
args = parser.parse_args()
total = 0
while True:
line = args.file.readline()
if not line:
if args.line_addresses:
print(f'{total:08x} ', end='')
if args.hex:
print(' '.join(f'{b:02x}' for b in line))
total += len(line)
if __name__ == '__main__':
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment