Created
July 26, 2016 21:56
-
-
Save tarunbod/e1958bf04e0b3534bde01cb44b67db76 to your computer and use it in GitHub Desktop.
Python command line hex-byte viewer
This file contains 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/env python | |
import os | |
from argparse import ArgumentParser | |
parser = ArgumentParser() | |
parser.add_argument("file", help="which file to view in hex format") | |
parser.add_argument("-f", "--head", help="if given, show only the first n*16 bytes", type=int) | |
parser.add_argument("-t", "--tail", help="if given, show only the last n*16 bytes", type=int) | |
parser.add_argument("-s", "--split", help="if given, show n bytes per line", type=int, default=16) | |
args = parser.parse_args() | |
with open(args.file, 'rb') as file: | |
contents = file.read() | |
if args.head: | |
contents = contents[0: (args.head) * args.split] | |
elif args.tail: | |
contents = contents[len(contents) - args.tail * args.split:] | |
size = len(contents) | |
print("Showing %d bytes:\n" % size) | |
for i in range(size): | |
print(hex(contents[i])[2:].zfill(2).upper(), end=' ' if i % args.split != args.split - 1 else '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment