Skip to content

Instantly share code, notes, and snippets.

@maple3142
Created July 7, 2021 03:20
Show Gist options
  • Save maple3142/080aab4942213d8dc8464b14d1efd005 to your computer and use it in GitHub Desktop.
Save maple3142/080aab4942213d8dc8464b14d1efd005 to your computer and use it in GitHub Desktop.
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