Created
March 12, 2021 17:33
-
-
Save jeffmikels/a170e57c59576e945242745c8cb2cc7d to your computer and use it in GitHub Desktop.
Python script to output anything as hex
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 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=' ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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