Base-utf8 encoding without escape sequences?

“1-char wide glyphs”? Okay, here’s a stupid strategy then.

data = b"put your arbitrary binary data here"
import base64
tr = str.maketrans('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
	''.join(chr(x) for x in range(0x300, 0x340)))
string = "a" + base64.b64encode(data).decode("ascii").translate(tr)

This gives you a VERY compact string literal - at least visually. To decode, reverse the procedure:

string = 'ȧ̴̜̥̯̝̗̈̇̕='
import base64
tr = str.maketrans(''.join(chr(x) for x in range(0x300, 0x340)),
	'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/')
data = base64.b64decode(string[1:].translate(tr).encode("ascii"))

Does that count?

2 Likes