Created
July 7, 2021 03:20
-
-
Save maple3142/080aab4942213d8dc8464b14d1efd005 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
import string | |
b64table = string.ascii_uppercase + string.ascii_lowercase + string.digits + "+/" | |
def to_utf7(s): | |
ret = "" | |
for c in s: | |
n = ord(c) | |
bits = f"{n:016b}00" | |
idxs = [int(bits[i : i + 6], 2) for i in range(0, len(bits), 6)] | |
ret += "+" + "".join(b64table[x] for x in idxs) + "-" | |
return ret | |
s = "Hello World!" | |
print(to_utf7(s)) | |
print(to_utf7(s).encode().decode("utf-7") == s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment