Skip to content

Instantly share code, notes, and snippets.

@farooqkz

farooqkz/hexview.py

Last active Dec 13, 2017
Embed
What would you like to do?
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