Skip to content

Instantly share code, notes, and snippets.

@jeffmikels
Created March 12, 2021 17:33
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 jeffmikels/a170e57c59576e945242745c8cb2cc7d to your computer and use it in GitHub Desktop.
Save jeffmikels/a170e57c59576e945242745c8cb2cc7d to your computer and use it in GitHub Desktop.
Python script to output anything as hex
#!/usr/bin/env python3
# usage:
# hex.py file1 file2
# cat file1 | hex.py
import os, sys
if len(sys.argv) > 1:
for fn in sys.argv[1:]:
print(fn)
with open(fn, 'rb') as f:
print(f.read().hex(' ', 1))
exit()
hasmore = True
while hasmore:
# os.read returns a bytes object
# sys.stdin returns a str object
# read one byte from file descriptor 0 (stdin)
b = os.read(0,1)
if b == b'':
print('')
exit()
print(b.hex(), end=' ')
@jeffmikels
Copy link
Author

I wanted to get just the header bytes from an item online, so I hacked this together. I used it like this:

curl -r 0-1023 URL | hex.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment