#!/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=' ')