Search code examples
pythonstructasciidiacriticsunpack

Python - Struct unpack bytes to ascii characters but with accents


I am trying to unpack some bytes with struct.unpack() in Python.

struct.unpack("xb4s", b'\x00\x04G\xe9g\xe9')

But I get : (4, b'G\xe9g\xe9') instead of (4, Gégé). This code does not handle accents. What should I do to have accents ?


Solution

  • Your string was was encoded with cp1252, so decode it appropriately.

    In [3]: struct.unpack("xb4s", b'\x00\x04G\xe9g\xe9')[1].decode('cp1252')
    Out[3]: 'Gégé'