Created
April 29, 2022 21:26
-
-
Save fbidu/28dfb1949756dd81a788cd58345aecfd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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