[Python] struct unpack di un intero
ciao a tutti! ho un problema con struct.unpack e non capisco cosa sto sbagliando... >>> struct.unpack("i", b'\x03\x00\x00\x00') # OK! (3,) >>> struct.unpack("ih", b'\x03\x00\x00\x00\x04\x00') # OK! (3, 4) >>> struct.unpack("ihi", b'\x03\x00\x00\x00\x04\x00\x00\x00\x04\x00') # ARGHHH Traceback (most recent call last): File "", line 1, in struct.error: unpack requires a buffer of 12 bytes Perché mai se ne aspetta 12? Dovrebbero bastare i 10 che ci sono nel buffer! Mi aspettavo: >>> struct.unpack("ihi", b'\x03\x00\x00\x00\x04\x00\x00\x00\x04\x00') (3, 4, 4) Cosa sto sbagliando? Marco ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] struct unpack di un intero
Ciao, perche' di default viene rispettato l'allineamento che il dato dovrebbe avere in memoria se fosse una struttura c: https://docs.python.org/3/library/struct.html#struct-alignment Roberto De Ioris Il giorno gio 21 ott 2021 alle ore 11:12 Marco De Paoli ha scritto: > ciao a tutti! > ho un problema con struct.unpack e non capisco cosa sto sbagliando... > > >>> struct.unpack("i", b'\x03\x00\x00\x00') # OK! > (3,) > >>> struct.unpack("ih", b'\x03\x00\x00\x00\x04\x00') # OK! > (3, 4) > >>> struct.unpack("ihi", b'\x03\x00\x00\x00\x04\x00\x00\x00\x04\x00') # > ARGHHH > Traceback (most recent call last): > File "", line 1, in > struct.error: unpack requires a buffer of 12 bytes > > Perché mai se ne aspetta 12? Dovrebbero bastare i 10 che ci sono nel > buffer! > > Mi aspettavo: > >>> struct.unpack("ihi", b'\x03\x00\x00\x00\x04\x00\x00\x00\x04\x00') > (3, 4, 4) > > Cosa sto sbagliando? > > Marco > ___ > Python mailing list > Python@lists.python.it > https://lists.python.it/mailman/listinfo/python > ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python
Re: [Python] struct unpack di un intero
ciao Roberto, Il giorno gio 21 ott 2021 alle ore 11:15 Roberto De Ioris ha scritto: > Ciao, perche' di default viene rispettato l'allineamento che il dato > dovrebbe avere in memoria se fosse una struttura c: > > https://docs.python.org/3/library/struct.html#struct-alignment > > Roberto De Ioris > infatti se poi facevo l'unpack come intero a partire dal quel byte, funzionava mi sfuggiva questo particolare di rispettare il layout dell'intera struct C grazie mille! Marco ___ Python mailing list Python@lists.python.it https://lists.python.it/mailman/listinfo/python