> For the complete documentation index, see [llms.txt](https://docs.urbit.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.urbit.org/hoon/stdlib/2p.md).

# 2p: Serialization

Serialization functions for packing and unpacking \`$noun\`s to and from atoms.

## `+cue` <a href="#cue" id="cue"></a>

Unpack `$atom` to `$noun`.

Produces a `$noun` unpacked from `$atom` `.a`. The inverse of [`+jam`](#jam).

#### Accepts

`.a` is an `$atom`.

#### Produces

A `$noun`.

#### Source

```hoon
++  cue
  ~/  %cue
  |=  a=@
  ^-  *
  =+  b=0
  =+  m=`(map @ *)`~
  =<  q
  |-  ^-  [p=@ q=* r=(map @ *)]
  ?:  =(0 (cut 0 [b 1] a))
    =+  c=(rub +(b) a)
    [+(p.c) q.c (~(put by m) b q.c)]
  =+  c=(add 2 b)
  ?:  =(0 (cut 0 [+(b) 1] a))
    =+  u=$(b c)
    =+  v=$(b (add p.u c), m r.u)
    =+  w=[q.u q.v]
    [(add 2 (add p.u p.v)) w (~(put by r.v) b w)]
  =+  d=(rub c a)
  [(add 2 p.d) (need (~(get by m) q.d)) m]
```

#### Examples

```
> (jam [1 2 3])
3.426.417
```

```
> (cue 3.426.417)
[1 2 3]
```

***

## `+jam` <a href="#jam" id="jam"></a>

Pack `$noun` to `$atom`.

Produces an `$atom` packed from `$noun` `.a`. The inverse of [`+cue`](#cue).

#### Accepts

`.a` is a `$noun`.

#### Produces

An `$atom`.

#### Source

```hoon
++  jam
  ~/  %jam
  |=  a=*
  ^-  @
  =+  b=0
  =+  m=`(map * @)`~
  =<  q
  |-  ^-  [p=@ q=@ r=(map * @)]
  =+  c=(~(get by m) a)
  ?~  c
    =>  .(m (~(put by m) a b))
    ?:  ?=(@ a)
      =+  d=(mat a)
      [(add 1 p.d) (lsh 0 q.d) m]
    =>  .(b (add 2 b))
    =+  d=$(a -.a)
    =+  e=$(a +.a, b (add b p.d), m r.d)
    [(add 2 (add p.d p.e)) (mix 1 (lsh [0 2] (cat 0 q.d q.e))) r.e]
  ?:  ?&(?=(@ a) (lte (met 0 a) (met 0 u.c)))
    =+  d=(mat a)
    [(add 1 p.d) (lsh 0 q.d) m]
  =+  d=(mat u.c)
  [(add 2 p.d) (mix 3 (lsh [0 2] q.d)) m]
```

#### Examples

```
> (jam 1)
12
```

```
> (cue 12)
1
```

```
> (jam [1 1])
817
```

```
> (cue 817)
[1 1]
```

```
> (jam [~ u=19])
39.689
```

```
> (cue 39.689)
[0 19]
```

***

## `+mat` <a href="#mat" id="mat"></a>

Length-encode.

Produces a cell whose tail `.q` is `$atom` `.a` with a bit representation of its length prepended to it (as the least significant bits). The head `.p` is the length of `.q` in bits.

#### Accepts

`.a` is an `$atom`.

#### Produces

A cell of two `$atom`s, `.p` and `.q`.

#### Source

```hoon
++  mat
  ~/  %mat
  |=  a=@
  ^-  [p=@ q=@]
  ?:  =(0 a)
    [1 1]
  =+  b=(met 0 a)
  =+  c=(met 0 b)
  :-  (add (add c c) b)
  (cat 0 (bex c) (mix (end [0 (dec c)] b) (lsh [0 (dec c)] a)))
```

#### Examples

```
> (mat 0xaaa)
[p=20 q=699.024]
```

```
> (met 0 q:(mat 0xaaa))
20
```

```
> `@ub`q:(mat 0xaaa)
0b1010.1010.1010.1001.0000
```

```
> =a =-(~&(- -) `@ub`0xaaa)
0b1010.1010.1010

> =b =-(~&(- -) `@ub`(xeb a))
0b1100

> =b =-(~&(- -) `@ub`(met 0 a))
0b1100

> =c =-(~&(- -) (xeb b))
4

> [`@ub`a `@ub`(end 0 (dec c) b) `@ub`(bex c)]
[0b1010.1010.1010 0b100 0b1.0000]
```

#### Discussion

`+mat` is only used internally as a helper to [`+jam`](#jam).

***

## `+rub` <a href="#rub" id="rub"></a>

Length-decode.

The inverse of `+mat`. Accepts a cell of index `.a` and a bitstring `.b` and produces the cell whose tail `.q` is the decoded `$atom` at index `.a` and whose head is the length of the encoded `$atom` `.q`, by which the offset `.a` is advanced.

#### Accepts

`.a` is an `$atom`.

`.b` is a bitstring as an `$atom`.

#### Produces

A cell of two `$atom`s, `.p` and `.q`.

#### Source

```hoon
++  rub
  ~/  %rub
  |=  [a=@ b=@]
  ^-  [p=@ q=@]
  =+  ^=  c
      =+  [c=0 m=(met 0 b)]
      |-  ?<  (gth c m)
      ?.  =(0 (cut 0 [(add a c) 1] b))
        c
      $(c +(c))
  ?:  =(0 c)
    [1 0]
  =+  d=(add a +(c))
  =+  e=(add (bex (dec c)) (cut 0 [d (dec c)] b))
  [(add (add c c) e) (cut 0 [(add d (dec c)) e] b)]
```

#### Examples

```
> `@ub`(jam 0xaaa)
0b1.0101.0101.0101.0010.0000
```

```
> (rub 1 0b1.0101.0101.0101.0010.0000)
[p=20 q=2.730]
```

```
> `@ux`q:(rub 1 0b1.0101.0101.0101.0010.0000)
0xaaa
```

#### Discussion

`+rub` is only used internally as a helper to [`+cue`](#cue).

***


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.urbit.org/hoon/stdlib/2p.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
