mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 12:40:15 +01:00
py/modstruct: Support pad bytes in struct format.
This adds support for the x format code in struct.pack and struct.unpack. The primary use case for this is ignoring bytes while unpacking. When interfacing with existing systems, it may often happen that you either have fields in a struct that aren't properly specified or you simply don't care about them. Being able to easily skip them is useful. Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
This commit is contained in:
committed by
Damien George
parent
e0a1480600
commit
d1f288c041
@@ -20,6 +20,8 @@ print(struct.pack("<h", 1))
|
||||
print(struct.pack(">h", 1))
|
||||
print(struct.pack("<b", 1))
|
||||
print(struct.pack(">b", 1))
|
||||
print(struct.pack("<x"))
|
||||
print(struct.pack(">x"))
|
||||
|
||||
print(struct.pack("<bI", -128, 256))
|
||||
print(struct.pack(">bI", -128, 256))
|
||||
@@ -29,6 +31,13 @@ print(struct.calcsize("97sI"))
|
||||
print(struct.unpack("<6sH", b"foo\0\0\0\x12\x34"))
|
||||
print(struct.pack("<6sH", b"foo", 10000))
|
||||
|
||||
print(struct.calcsize("7xx"))
|
||||
print(struct.pack("7xx"))
|
||||
|
||||
print(struct.calcsize(">bxI3xH"))
|
||||
print(struct.pack(">bxI3xH", 1, 2, 3))
|
||||
print(struct.unpack(">bxI3xH", b"\x01\0\0\0\0\x02\0\0\0\0\x03"))
|
||||
|
||||
s = struct.pack("BHBI", 10, 100, 200, 300)
|
||||
v = struct.unpack("BHBI", s)
|
||||
print(v == (10, 100, 200, 300))
|
||||
|
||||
Reference in New Issue
Block a user