Created
March 19, 2023 08:18
-
-
Save aoirint/2ef1cc895321a3a784dd8e01878a33ed 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
def decode_utf8_bytestream(byte_stream): | |
i = 0 | |
decoded_strings = [] | |
decoded_string = b'' | |
while i < len(byte_stream): | |
byte = byte_stream[i] | |
if byte == 0x00: | |
decoded_strings.append(decoded_string) | |
decoded_string = b'' | |
i += 1 | |
elif byte <= 0x7F: | |
decoded_string += byte_stream[i:i+1] | |
i += 1 | |
elif byte <= 0xDF: | |
decoded_string += byte_stream[i:i+2] | |
i += 2 | |
elif byte <= 0xEF: | |
decoded_string += byte_stream[i:i+3] | |
i += 3 | |
else: | |
decoded_string += byte_stream[i:i+4] | |
i += 4 | |
if len(decoded_string) != 0: | |
decoded_strings.append(decoded_string) | |
return [s.decode('utf-8') for s in decoded_strings] | |
byte_str = b'\xe3\x81\x82\xe3\x81\x84\xe3\x81\x86\x00\xe3\x81\x8a\xe3\x81\x86\xe3\x81\x8c\xe3\x81\x84\x00' | |
decoded_strings = decode_utf8_bytestream(byte_str) | |
print(decoded_strings[0]) # 'あいう' | |
print(decoded_strings[1]) # 'おうがい' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実際には、
が出力されるので、まだ動いていなさそう