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.
good to know
Section titled “good to know”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
p8x as 1 byte, with -128 to -1 come out in two’s complementp16x as 2 bytes, lowest byte first, or highest first if order is “big”p32x as 4 bytes, lowest byte first, or highest first if order is “big”p64x as 8 bytes, lowest byte first, or highest first if order is “big”u81 byte back to an integer from 0 to 255u162 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”u324 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”u648 bytes back to a non-negative integer, lowest byte first, or highest first if order is “big”xorxor 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 bisa ^ bAn int key is a single byte. Xor-ing again with the same key gives back the originalcyclicn 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 wherecyclic_findhow many bytes into the cyclic pattern sub appears, or nil
Functions
Section titled “Functions”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
1p8(0x41)b"A"2p8(-1)b"\xff"p16(x: int, order: str = “little”) -> bytes
x as 2 bytes, lowest byte first, or highest first if order is “big”
1p16(0x4142)b"BA"2p16(443, big)b"\x01\xbb"p32(x: int, order: str = “little”) -> bytes
x as 4 bytes, lowest byte first, or highest first if order is “big”
1p32(0xdeadbeef)b"\xef\xbe\xad\xde"2p32(-2)b"\xfe\xff\xff\xff"p64(x: int, order: str = “little”) -> bytes
x as 8 bytes, lowest byte first, or highest first if order is “big”
1p64(0x401000)b"\0\x10@\0\0\0\0\0"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
1u8(b"A")65See 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”
1u16(b"\x01\xbb", big)443See 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”
1u32(b"\xef\xbe\xad\xde") to hex0xdeadbeefSee 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”
1u64(p64(-1))18446744073709551615See 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
1b"hello".xor(0x20)b"HELLO"2"secret".xor("k").xor("k").str"secret"See also bytes
cyclic
Section titled “cyclic”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
1cyclic(16)b"aaaabaaacaaadaaa"See also cyclic_find
cyclic_find
Section titled “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
1cyclic_find(b"baaa")42cyclic_find(0x6161616c)44See also cyclic
More examples
Section titled “More examples”packing
Section titled “packing”1# an address, lowest byte first2p32(0xdeadbeef)b"\xef\xbe\xad\xde"3# and back4u32(b"\xef\xbe\xad\xde") to hex0xdeadbeef5# a port in network byte order6p16(443, big)b"\x01\xbb"7# how far into the input did the crash land?8cyclic_find(0x6161616c)44Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.