Skip to content

dev.packing

integers as raw bytes and back, xor, and crash-offset patterns, as in pwntools

Integers as the raw bytes a CPU stores for them, and back, as when writing exploits for CTF challenges. Integers pack into 1, 2, 4 or 8 bytes, and bytes of those sizes read back into an integer.

Most CPUs, including x86 and ARM, store the lowest byte first, which is called little-endian, so that’s the default. Pass big for big-endian, the order network protocols use. Negative numbers come out in two’s complement, the way CPUs store them, so -1 in one byte is 0xff.

Tags: encoding, conversion, binary

  • p8 x as 1 byte, with -128 to -1 come out in two’s complement
  • p16 x as 2 bytes, lowest byte first, or highest first if order is “big”
  • p32 x as 4 bytes, lowest byte first, or highest first if order is “big”
  • p64 x as 8 bytes, lowest byte first, or highest first if order is “big”
  • u8 1 byte back to an integer from 0 to 255
  • u16 2 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”
  • u32 4 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”
  • u64 8 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”
  • xor xor each byte with the key, repeating the key as often as needed An int key is a single byte. Xor-ing again with the same key gives back the original. Between two ints, a xor b is a ^ b An int key is a single byte. Xor-ing again with the same key gives back the original
  • cyclic n bytes of a pattern in which every run of 4 bytes is different (a de Bruijn sequence), to send to a program with a buffer overflow and see which part ends up where
  • cyclic_find how many bytes into the cyclic pattern sub appears, or nil
p8(x: int, order: str = “little”) -> bytes

x as 1 byte, with -128 to -1 come out in two’s complement

order is “little” or “big”, though one byte reads the same either way

1
p8(0x41)b"A"
2
p8(-1)b"\xff"
Example outputCtrl+Enter to run

See also u8, p32

p16(x: int, order: str = “little”) -> bytes

x as 2 bytes, lowest byte first, or highest first if order is “big”

1
p16(0x4142)b"BA"
2
p16(443, big)b"\x01\xbb"
Example outputCtrl+Enter to run

See also u16, p32

p32(x: int, order: str = “little”) -> bytes

x as 4 bytes, lowest byte first, or highest first if order is “big”

1
p32(0xdeadbeef)b"\xef\xbe\xad\xde"
2
p32(-2)b"\xfe\xff\xff\xff"
Example outputCtrl+Enter to run

See also u32, p64

p64(x: int, order: str = “little”) -> bytes

x as 8 bytes, lowest byte first, or highest first if order is “big”

1
p64(0x401000)b"\0\x10@\0\0\0\0\0"
Example outputCtrl+Enter to run

See also u64, p32

u8(b: bytes, order: str = “little”) -> int

1 byte back to an integer from 0 to 255

order is “little” or “big”, though one byte reads the same either way

1
u8(b"A")65
Example outputCtrl+Enter to run

See also p8

u16(b: bytes, order: str = “little”) -> int

2 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”

1
u16(b"\x01\xbb", big)443
Example outputCtrl+Enter to run

See also p16

u32(b: bytes, order: str = “little”) -> int

4 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”

1
u32(b"\xef\xbe\xad\xde") to hex0xdeadbeef
Example outputCtrl+Enter to run

See also p32

u64(b: bytes, order: str = “little”) -> int

8 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”

1
u64(p64(-1))18446744073709551615
Example outputCtrl+Enter to run

See also p64

xor(b: bytes|str, key: bytes|str|int) -> bytes

xor each byte with the key, repeating the key as often as needed An int key is a single byte. Xor-ing again with the same key gives back the original. Between two ints, a xor b is a ^ b An int key is a single byte. Xor-ing again with the same key gives back the original

1
b"hello".xor(0x20)b"HELLO"
2
"secret".xor("k").xor("k").str"secret"
Example outputCtrl+Enter to run

See also bytes

cyclic(n: int) -> bytes

n bytes of a pattern in which every run of 4 bytes is different (a de Bruijn sequence), to send to a program with a buffer overflow and see which part ends up where

1
cyclic(16)b"aaaabaaacaaadaaa"
Example outputCtrl+Enter to run

See also cyclic_find

cyclic_find(sub: bytes|str|int) -> int|nil

how many bytes into the cyclic pattern sub appears, or nil

After a crash, give it the value a register ended up holding to learn how many bytes into your input that was. An int is taken as the bytes it would be in memory, lowest first: 4 of them, or 8 if it’s too big for 4, as when you copy a register’s value from a debugger

1
cyclic_find(b"baaa")4
2
cyclic_find(0x6161616c)44
Example outputCtrl+Enter to run

See also cyclic

1
# an address, lowest byte first
2
p32(0xdeadbeef)b"\xef\xbe\xad\xde"
3
# and back
4
u32(b"\xef\xbe\xad\xde") to hex0xdeadbeef
5
# a port in network byte order
6
p16(443, big)b"\x01\xbb"
7
# how far into the input did the crash land?
8
cyclic_find(0x6161616c)44
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.