Skip to content

Instantly share code, notes, and snippets.

@jinie
Created July 5, 2011 18:25
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jinie/1065494 to your computer and use it in GitHub Desktop.
Save jinie/1065494 to your computer and use it in GitHub Desktop.
Read binary file in python and print hex output
#!/usr/bin/python
import sys,getopt
filename = None
blocksize = 1024
opts,args = getopt.getopt(sys.argv[1:],'f:b:')
for o,a in opts:
if o == '-f':
filename = a
if o == '-b':
blocksize = a
offset = 0
with open(filename,"rb") as f:
block = f.read(blocksize)
str = ""
for ch in block:
str += hex(ord(ch))+" "
print str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment