2015-05-04 13:35:40 +00:00
|
|
|
try:
|
|
|
|
import ustruct as struct
|
|
|
|
except:
|
|
|
|
import struct
|
2014-04-10 00:45:38 +00:00
|
|
|
print(struct.calcsize("<bI"))
|
|
|
|
print(struct.unpack("<bI", b"\x80\0\0\x01\0"))
|
2014-04-10 19:19:32 +00:00
|
|
|
print(struct.calcsize(">bI"))
|
|
|
|
print(struct.unpack(">bI", b"\x80\0\0\x01\0"))
|
2014-04-11 00:47:21 +00:00
|
|
|
|
|
|
|
# 32-bit little-endian specific
|
|
|
|
#print(struct.unpack("bI", b"\x80\xaa\x55\xaa\0\0\x01\0"))
|
2014-04-19 00:13:15 +00:00
|
|
|
|
|
|
|
print(struct.pack("<i", 1))
|
|
|
|
print(struct.pack(">i", 1))
|
|
|
|
print(struct.pack("<h", 1))
|
|
|
|
print(struct.pack(">h", 1))
|
|
|
|
print(struct.pack("<b", 1))
|
|
|
|
print(struct.pack(">b", 1))
|
|
|
|
|
|
|
|
print(struct.pack("<bI", -128, 256))
|
|
|
|
print(struct.pack(">bI", -128, 256))
|
2014-05-12 20:45:50 +00:00
|
|
|
|
|
|
|
print(struct.calcsize("100sI"))
|
|
|
|
print(struct.calcsize("97sI"))
|
|
|
|
print(struct.unpack("<6sH", b"foo\0\0\0\x12\x34"))
|
|
|
|
print(struct.pack("<6sH", b"foo", 10000))
|
2014-06-25 19:25:53 +00:00
|
|
|
|
|
|
|
s = struct.pack("BHBI", 10, 100, 200, 300)
|
|
|
|
v = struct.unpack("BHBI", s)
|
|
|
|
print(v == (10, 100, 200, 300))
|
2014-12-05 23:13:52 +00:00
|
|
|
|
|
|
|
# check maximum pack on 32-bit machine
|
|
|
|
print(struct.pack("<I", 2**32 - 1))
|
|
|
|
print(struct.pack("<I", 0xffffffff))
|
|
|
|
|
2015-04-25 22:16:39 +00:00
|
|
|
# long long ints
|
|
|
|
print(struct.pack("<Q", 2**64 - 1))
|
|
|
|
print(struct.pack("<Q", 0xffffffffffffffff))
|
|
|
|
print(struct.pack("<q", -1))
|
|
|
|
print(struct.pack("<Q", 1234567890123456789))
|
|
|
|
print(struct.pack("<q", -1234567890123456789))
|
|
|
|
print(struct.pack(">Q", 1234567890123456789))
|
|
|
|
print(struct.pack(">q", -1234567890123456789))
|
|
|
|
print(struct.unpack("<Q", b"\x12\x34\x56\x78\x90\x12\x34\x56"))
|
|
|
|
print(struct.unpack(">Q", b"\x12\x34\x56\x78\x90\x12\x34\x56"))
|
|
|
|
print(struct.unpack("<q", b"\x12\x34\x56\x78\x90\x12\x34\xf6"))
|
|
|
|
print(struct.unpack(">q", b"\xf2\x34\x56\x78\x90\x12\x34\x56"))
|
2014-12-05 23:13:52 +00:00
|
|
|
|
|
|
|
# check maximum unpack
|
|
|
|
print(struct.unpack("<I", b"\xff\xff\xff\xff"))
|
|
|
|
print(struct.unpack("<Q", b"\xff\xff\xff\xff\xff\xff\xff\xff"))
|
2015-04-04 23:03:43 +00:00
|
|
|
|
|
|
|
# network byte order
|
|
|
|
print(struct.pack('!i', 123))
|