Skip to content

Instantly share code, notes, and snippets.

@aarmn
Created January 10, 2025 18:42
Show Gist options
  • Save aarmn/f6b8b3c904f4bdf6456aee6379f21f98 to your computer and use it in GitHub Desktop.
Save aarmn/f6b8b3c904f4bdf6456aee6379f21f98 to your computer and use it in GitHub Desktop.
Gets a bytestream of utf8 in reverse and stream it back to text
import time
def reverse_utf8_decoder(input_string):
encoded_bytes = input_string.encode("utf-8")
for byte in reversed(encoded_bytes):
yield byte
time.sleep(1)
def reverse_utf8_char_decoder(byte_stream):
buffer = []
for byte in byte_stream:
buffer.insert(0, byte)
if len(buffer) >= 4 or (len(buffer) >= 1 and (buffer[0] & 0b11000000) != 0b10000000):
try:
char = bytes(buffer).decode("utf-8")
yield char
buffer.clear()
except UnicodeDecodeError:
continue
def main():
input_string = "𐭡𐭢𐭣𐭤𐭥𐭦𐭧𐭨𐭩𐭪𒀱𒀱𒀱"
byte_stream = reverse_utf8_decoder(input_string)
char_stream = reverse_utf8_char_decoder(byte_stream)
for char in char_stream:
print(char, end="")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment