pack and unpack at the right format in python
<p>I'm looking to unpack from a buffer a string and its length.</p>

<blockquote>
  <p>For example to obtain (4, 'GÃ©gÃ©') from this buffer :
  b'\x00\x04G\xE9g\xe9'</p>
</blockquote>

<p>Does someone know how to do ?</p>

<p>The length data looks like a big-endian unsigned 16 bit integer, and the string data looks like it's using the Latin1 encoding. If that's correct, you can extract it like this:</p>

<pre><code>from struct import unpack

def extract(buff):
    return unpack(b'&gt;H', buff[:2])[0], buff[2:].decode('latin1')

buff = b'\x00\x04G\xE9g\xe9'
print(extract(buff))
</code></pre>

<p><strong>output</strong></p>

<pre><code>(4, 'GÃ©gÃ©')
</code></pre>

python python-3.x pack unpack