Skip to content

Instantly share code, notes, and snippets.

@jeffmikels
Created March 12, 2021 17:33
Show Gist options
  • Save jeffmikels/a170e57c59576e945242745c8cb2cc7d to your computer and use it in GitHub Desktop.
Save jeffmikels/a170e57c59576e945242745c8cb2cc7d to your computer and use it in GitHub Desktop.

Revisions

  1. jeffmikels revised this gist Mar 12, 2021. No changes.
  2. jeffmikels created this gist Mar 12, 2021.
    26 changes: 26 additions & 0 deletions hex.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #!/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=' ')