I have a piece of code that reads binary data from a string buffer (object StringIO) and tries to convert it to an object bytearray, but it throws errors when the value is greater than 127, which the ascii encoding cannot handle, even when I try to override it:
file = open(filename, 'r+b')
file.seek(offset)
chunk = file.read(length)
chunk = zlib.decompress(chunk)
chunk = StringIO(chunk)
d = bytearray(chunk.read(10), encoding="iso8859-1", errors="replace")
Running this code gives me:
d = bytearray(chunk.read(10), encoding="iso8859-1", errors="replace")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 3: ordinal not in range(128)
Obviously, 240 (decimal 0xf0) cannot fit into the ascii coding range, but therefore I explicitly set the coding. But this seems to ignore him.
source
share