> For the complete documentation index, see [llms.txt](https://ir0nstone.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ir0nstone.gitbook.io/notes/misc/pwntools/packing.md).

# Packing

Packing with the in-built python `struct` module is often a pain with loads of unnecessary options to remember. pwntools makes this a breeze, using the `context` global variable to automatically calculate how the packing should work.

## p64(addr)

Packs `addr` depending on `context`, which by default is **little-endian**.<br>

```python
p64(0x04030201) == b'\x01\x02\x03\x04'

context.endian = 'big'
p64(0x04030201) == b'\x04\x03\x02\x01'
```

{% hint style="info" %}
`p64()` returns a bytes-like object, so you'll have to form your padding as `b'A'` instead of just `'A'`.
{% endhint %}

## u64(data)

Unpacks `data` depending on `context`; exact opposite of `p64()`.

## flat(\*args)

Can take a bunch of arguments and packs them all according to `context`. The full functionality is quite [complex](http://docs.pwntools.com/en/stable/util/packing.html#pwnlib.util.packing.flat), but essentially:

```python
payload = flat(
    0x01020304,
    0x59549342,
    0x12186354
)
```

is equivalent to

```python
payload = p64(0x01020304) + p64(0x59549342) + p64(0x12186354)
```

{% hint style="danger" %}
`flat()` uses `context`, so unless you specify that it is 64 bits it will attempt to pack it as 32 bits.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ir0nstone.gitbook.io/notes/misc/pwntools/packing.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
