26 lines
722 B
Python
26 lines
722 B
Python
#!/usr/bin/env python3
|
|
"""Restore bytes from an ASCII 0/1 binary-text representation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
from pathlib import Path
|
|
|
|
|
|
def main() -> None:
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("input", type=Path)
|
|
parser.add_argument("output", type=Path)
|
|
args = parser.parse_args()
|
|
|
|
text = args.input.read_text(encoding="ascii")
|
|
bits = "".join(ch for ch in text if ch in "01")
|
|
if len(bits) % 8 != 0:
|
|
raise SystemExit(f"Invalid binary text length: {len(bits)} bits is not divisible by 8")
|
|
data = bytes(int(bits[i : i + 8], 2) for i in range(0, len(bits), 8))
|
|
args.output.write_bytes(data)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|