Ad

UnicodeDecodeError: 'utf-8' Codec Can't Decode Byte 0x9d In Position 0: Invalid Start Byte When I Execute The ` B.decode()`

- 1 answer

I have a code like bellow:

cn_bytes = [157, 188, 156]

cn_str = ""
clen = len(cn_bytes)
count = int(clen / 3)
for x in range(count):
    i = x * 3
    b = bytes([cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]])

    print(b)

    cn_str += b.decode()

when I execute it, I will get bellow error:

Traceback (most recent call last):
  File "/Users/dele/Desktop/TestIOS/TestPython/testDemo01/testDemo01/test08.py", line 30, in <module>
    cn_str += b.decode()
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte

there says:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x9d in position 0: invalid start byte

Ad

Answer

Actually I think you can't convert entire list as bytes using bytes() . But still You can convert it into bytes by iterate through individual elements.

b = [cn_bytes[i], cn_bytes[i + 1], cn_bytes[i + 2]]

cn_str =  [bytes(i)for i in b]
Ad
source: stackoverflow.com
Ad