Python bytearray ignoring encoding? -


i've got chunk of code reads binary data off string buffer (stringio object), , tries convert bytearray object, it's throwing errors when value greater 127, ascii encoding can't handle, when i'm trying 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 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 of 0xf0) can't fit in ascii encoding range, that's why i'm explicitly setting encoding. seems ignoring it.

when converting string encoding, original encoding taken ascii if str or unicode if unicode object. when creating bytearray, encoding parameter required if string unicode. don't specify encoding , results want.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -