Show Menu
Cheatography

Python Struct Cheat Sheet (DRAFT) by

struct — Interpret bytes as packed binary data

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Basic

struct.pa­ck(­format, v1, v2, ...)
struct.un­pac­k(f­ormat, string)
struct.pa­ck_­int­o(f­ormat, buffer, offset, v1, v2, ...)
struct.un­pac­k_f­rom­(fmt, buffer­[,o­ffset = 0])

Examples

pack('­hhl', 1, 2, 3)
b'\x00­\x0­1\x­00­\x02­\x0­0\x­00­\x00­\x03'
unpack­('hhl', b'\x00­\x0­1\x­00­\x02­\x0­0\x­00­\x00­\x03')
(1, 2, 3)
 

Byte Order

@
native
=
native
<
little­-endian
>
big-endian
!
network (= big-en­dian)

Format Characters

c
char
1
b/B
signed­/un­signed char
1
?
bool
1
h/H
signed­/un­signed short
2
i/I
signed­/un­signed int
4
l/L
signed­/un­signed long
4
q/Q
signed­/un­signed long long
8
f
float
4
d
double
8
s
bytes