Skip to content

Instantly share code, notes, and snippets.

@taikedz
Last active May 10, 2021 10:47
Show Gist options
  • Save taikedz/8c1881d11479696cde9f911fade0ed0e to your computer and use it in GitHub Desktop.
Save taikedz/8c1881d11479696cde9f911fade0ed0e to your computer and use it in GitHub Desktop.
Bitstrings converter

Bitstrings converter

Probably many people have written something like this, for converting those joke images purporting to contain L3E7 h4xx0R c0deZ.

Data examples:

  • ascii-bits.txt: taken from a random pic of a mini typewriter with a 1 and 0 keyboard.
  • message.txt: taken from a music video ad with an AI-type character

Script:

  • Takes a text file with typed 1 and 0 characters to represent bits.
  • Strips all whitespace
  • Converts them to 8-bit bytes, and prints the results.
  • Two types of padding are done in case the number of bits is not a multiple of 8.
  • Optionally specify an encoding to use for string printing.

A naive, dirty implementation whacked together in a few minutes.

python3 convert-bitstring.py ascii-bits.txt [ENCODING]
1001010101
0110001010
1100111000
1001100111
0111000110
1110101010
0011011000
1111010011
01001
import sys
import re
def getbyte(oz, rfill=False):
if len(oz) != 8:
if rfill:
oz = oz.zfill(8)
else:
oz = oz.ljust(8, '0')
print(f"Final byte: {oz}")
return int(oz, 2)
def fillprint(direction, oz_chunks):
left = direction == "left"
fillname = "Big endian" if left else "Little endian"
fillname += " padding on final byte"
print(f"\n{fillname}:")
mybytes = []
for oz in oz_chunks:
mybytes.append(getbyte(oz, left))
decode = None if len(sys.argv) < 3 else sys.argv[2]
if decode:
print(str(bytes(mybytes), decode) )
else:
print(str(bytes(mybytes) ))
def main():
lines = None
with open(sys.argv[1]) as fh:
lines = fh.readlines()
lines = [re.sub(r'\s', '', theline) for theline in lines]
onezeroes = ''.join(lines)
print("%i : %s" % (len(onezeroes), onezeroes) )
chunks, chunksize = len(onezeroes), 8
oz_chunks = [ onezeroes[i:i+chunksize] for i in range(0, chunks, chunksize) ]
fillprint("right", oz_chunks)
fillprint("left", oz_chunks)
if __name__ == "__main__":
main()
01010100 01100101 01101100 01101100 00100000 01101101 01100101 00100000 01101000 01101111 01110111 00100000 01111001 01101111 01110101 00100000 01100110 01100101 01100101 01101100
@taikedz
Copy link
Author

taikedz commented Aug 12, 2020

Output for ascii-bits.txt:

85 : 1001010101011000101011001110001001100111011100011011101010100011011000111101001101001

Little endian padding on final byte:
Final byte: 01001000
b'\x95X\xac\xe2gq\xba\xa3c\xd3H'

Big endian padding on final byte:
Final byte: 00001001
b'\x95X\xac\xe2gq\xba\xa3c\xd3\t'

Output for message.txt:

160 : 0101010001100101011011000110110000100000011011010110010100100000011010000110111101110111001000000111100101101111011101010010000001100110011001010110010101101100

Little endian padding on final byte:
b'Tell me how you feel'

Big endian padding on final byte:
b'Tell me how you feel'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment