Skip to content

Instantly share code, notes, and snippets.

@highfestiva
Created December 14, 2025 19:47
Show Gist options
  • Select an option

  • Save highfestiva/ade021a045f3aa02059a75bf24e4e18b to your computer and use it in GitHub Desktop.

Select an option

Save highfestiva/ade021a045f3aa02059a75bf24e4e18b to your computer and use it in GitHub Desktop.
Translate binary to ASCII, use bit offset 0 through 7 and print all permutations
#!/usr/bin/env python3
import sys
f = sys.stdout
# f = open('code.txt', 'wt', encoding='utf-8')
def print_bits(bits):
# store as bytes first
b = b''
for i in range(0, len(bits), 8):
byte = bits[i:i+8]
b += bytes([int(byte, 2)])
# decode to string one or several at a time
s = ''
while b:
for i in range(1,5):
try:
s += b[:i].decode()
b = b[i:]
break
except:
pass
else:
s += '?'
b = b[1:]
print(s, file=f, flush=True)
bits = ""
while True:
b = input("bit (0/1, empty to quit): ")
bb = ''
for c in b:
if c not in ("0", "1"):
continue
bb += c
bits += bb
print(bits)
for i in range(8):
print(i, end=' ', file=f)
print_bits(bits[i:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment