Skip to content

Instantly share code, notes, and snippets.

@farooqkz
Last active December 13, 2017 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farooqkz/28ea70a657f09588faacc95ad3c7b7e2 to your computer and use it in GitHub Desktop.
Save farooqkz/28ea70a657f09588faacc95ad3c7b7e2 to your computer and use it in GitHub Desktop.
simple file viewer in hex format. usage: hexview.py <filename>
#!/usr/bin/python3
'''
HexView.Py - A simple File Viewer in Hexadecimal format.
Author: FarooqKZ
Under GPL3+. THIS SOFTWARE COMES WITH NO WARRENTY FROM MAIN AUTHOR.
'''
import sys # I inlcude this to use sys.exit() and sys.argv
def showhelp(): # this function shows usage of program
print("Usage: hexview.py file_path\n\tfile_path: Path of file to view.")
sys.exit()
if '-h' in sys.argv:
# shows help if something like this passed to program: 'hexview.py -h'
showhelp()
path = sys.argv[1]
filestream = None
fbytes = None
try:
filestream = open(path, 'br')
fbytes = filestream.read() # Read all bytes from the file
finally:
if filestream:
filestream.close()
rown = 0
print('')
for b in fbytes:
s = hex(b).upper()
s = s.replace('0X', '') # "0XBA" => "BA"
if len(s) == 1:
print('0', end='')
print(s, end=' ')
rown += 1
if (rown % 27) == 0:
print('')
print("\n") # prints 2 newline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment