Skip to content

Instantly share code, notes, and snippets.

@BurhanH
Created May 20, 2019 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BurhanH/b577258e2ddf78ffd980bb2c67cb2bf5 to your computer and use it in GitHub Desktop.
Save BurhanH/b577258e2ddf78ffd980bb2c67cb2bf5 to your computer and use it in GitHub Desktop.
Convert a character into 8 bits (https://realpython.com/python-encodings-guide/)
# python 3.7
def make_bitseq(s: str) -> str:
if not s.isascii():
raise ValueError("ASCII only allowed")
return " ".join(f"{ord(i):08b}" for i in s)
make_bitseq("bits")
# '01100010 01101001 01110100 01110011'
make_bitseq("CAPS")
# '01000011 01000001 01010000 01010011'
make_bitseq("$25.43")
# '00100100 00110010 00110101 00101110 00110100 00110011'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment