Skip to content

Instantly share code, notes, and snippets.

@fbidu
Created April 29, 2022 21:26
Show Gist options
  • Save fbidu/28dfb1949756dd81a788cd58345aecfd to your computer and use it in GitHub Desktop.
Save fbidu/28dfb1949756dd81a788cd58345aecfd to your computer and use it in GitHub Desktop.
"""
Data are bits
Numbers can be written as bits
Therefore, any data can be written as a number
Here is a small playground
"""
def text_to_int(filename):
"""
Converts a UTF-8 encoded text file into an integer
"""
data = open(filename).read()
data = data.encode("utf-8")
return int(data.hex(), 16)
def int_to_text(n):
"""
Converts an integer into a UTF-8 encoded text
"""
data = n.to_bytes((n.bit_length() + 7) // 8, "big")
data = data.decode("utf-8")
return data
if __name__ == "__main__":
print("This code, as a huge integer:")
print(text_to_int("all_ints.py"))
print("\nThat huge integer as text:")
print(int_to_text(text_to_int("all_ints.py")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment