# Module sui::bcs

*[Documentation index](/llms.txt) · [Full index](/llms-full.txt)*

This module implements BCS (de)serialization in Move.

Full specification can be found here: https://github.com/diem/bcs

Short summary (for Move-supported types):

- address - sequence of X bytes
- bool - byte with 0 or 1
- u8 - a single u8 byte
- u16 / u32 / u64 / u128 / u256 - LE bytes
- vector - ULEB128 length + LEN elements
- option - first byte bool: None (0) or Some (1), then value

Usage example:
```
/// This function reads u8 and u64 value from the input
/// and returns the rest of the bytes.
fun deserialize(bytes: vector<u8>): (u8, u64, vector<u8>) {
    use sui::bcs::\{Self, BCS\};

    let prepared: BCS = bcs::new(bytes);
    let (u8_value, u64_value) = (
        prepared.peel_u8(),
        prepared.peel_u64()
    );

    // unpack bcs struct
    let leftovers = prepared.into_remainder_bytes();

    (u8_value, u64_value, leftovers)
}
```

```
use std::ascii;
use std::bcs;
use std::option;
use std::string;
use std::vector;
use sui::address;
use sui::hex;
```

## Struct BCS

A helper struct that saves resources on operations. For better
vector performance, it stores reversed bytes of the BCS and
enables use of vector::pop_back.

```
public struct BCS has copy, drop, store
```

**Fields**

**bytes: vector&lt;u8&gt;**

## Constants

For when bytes length is less than required for deserialization.

```
const EOutOfRange: u64 = 0;
```

For when the boolean value different than 0 or 1.

```
const ENotBool: u64 = 1;
```

For when ULEB byte is out of range (or not found).

```
const ELenOutOfRange: u64 = 2;
```

## Function to_bytes

Get BCS serialized bytes for any value.

Re-exports stdlib [bcs::to_bytes](../sui_sui/bcs#sui_bcs_to_bytes).

```
public fun to_bytes&lt;T&gt;(value: &T): vector&lt;u8&gt;
```

## Function new

Creates a new instance of BCS wrapper that holds inversed
bytes for better performance.

```
public fun new(bytes: vector&lt;u8&gt;): sui::bcs::BCS
```

## Function into_remainder_bytes

Unpack the [BCS](../sui_sui/bcs#sui_bcs_BCS) struct returning the leftover bytes.

Useful for passing the data further after partial deserialization.

```
public fun into_remainder_bytes(bcs: sui::bcs::BCS): vector&lt;u8&gt;
```

## Function peel_address

Read address from the bcs-serialized bytes.

```
public fun peel_address(bcs: &mut sui::bcs::BCS): address
```

## Function peel_bool

Read a bool value from bcs-serialized bytes.

```
public fun peel_bool(bcs: &mut sui::bcs::BCS): bool
```

## Function peel_u8

Read u8 value from bcs-serialized bytes.

```
public fun peel_u8(bcs: &mut sui::bcs::BCS): u8
```

## Macro function peel_num

```
macro fun peel_num&lt;$I, $T&gt;($bcs: &mut sui::bcs::BCS, $len: u64, $bits: $I): $T
```

## Function peel_u16

Read u16 value from bcs-serialized bytes.

```
public fun peel_u16(bcs: &mut sui::bcs::BCS): u16
```

## Function peel_u32

Read u32 value from bcs-serialized bytes.

```
public fun peel_u32(bcs: &mut sui::bcs::BCS): u32
```

## Function peel_u64

Read u64 value from bcs-serialized bytes.

```
public fun peel_u64(bcs: &mut sui::bcs::BCS): u64
```

## Function peel_u128

Read u128 value from bcs-serialized bytes.

```
public fun peel_u128(bcs: &mut sui::bcs::BCS): u128
```

## Function peel_u256

Read u256 value from bcs-serialized bytes.

```
public fun peel_u256(bcs: &mut sui::bcs::BCS): u256
```

## Function peel_vec_length

Read ULEB bytes expecting a vector length. Result should
then be used to perform peel_* operation LEN times.

In BCS vector length is implemented with ULEB128;

See more here: https://en.wikipedia.org/wiki/LEB128

```
public fun peel_vec_length(bcs: &mut sui::bcs::BCS): u64
```

## Macro function peel_vec

Peel vector&lt;&#36;T&gt; from serialized bytes, where &#36;peel: |&**mut** [BCS](../sui_sui/bcs#sui_bcs_BCS)| -&gt; &#36;T gives the
functionality of peeling each value.

```
public macro fun peel_vec&lt;$T&gt;($bcs: &mut sui::bcs::BCS, $peel: |&mut sui::bcs::BCS| -&gt; $T): vector&lt;$T&gt;
```

## Function peel_vec_address

Peel a vector of **address** from serialized bytes.

```
public fun peel_vec_address(bcs: &mut sui::bcs::BCS): vector&lt;address&gt;
```

## Function peel_vec_bool

Peel a vector of **address** from serialized bytes.

```
public fun peel_vec_bool(bcs: &mut sui::bcs::BCS): vector&lt;bool&gt;
```

## Function peel_vec_u8

Peel a vector of u8 (eg string) from serialized bytes.

```
public fun peel_vec_u8(bcs: &mut sui::bcs::BCS): vector&lt;u8&gt;
```

## Function peel_vec_vec_u8

Peel a vector&lt;vector&lt;u8&gt;&gt; (eg vec of string) from serialized bytes.

```
public fun peel_vec_vec_u8(bcs: &mut sui::bcs::BCS): vector&lt;vector&lt;u8&gt;&gt;
```

## Function peel_vec_u16

Peel a vector of u16 from serialized bytes.

```
public fun peel_vec_u16(bcs: &mut sui::bcs::BCS): vector&lt;u16&gt;
```

## Function peel_vec_u32

Peel a vector of u32 from serialized bytes.

```
public fun peel_vec_u32(bcs: &mut sui::bcs::BCS): vector&lt;u32&gt;
```

## Function peel_vec_u64

Peel a vector of u64 from serialized bytes.

```
public fun peel_vec_u64(bcs: &mut sui::bcs::BCS): vector&lt;u64&gt;
```

## Function peel_vec_u128

Peel a vector of u128 from serialized bytes.

```
public fun peel_vec_u128(bcs: &mut sui::bcs::BCS): vector&lt;u128&gt;
```

## Function peel_vec_u256

Peel a vector of u256 from serialized bytes.

```
public fun peel_vec_u256(bcs: &mut sui::bcs::BCS): vector&lt;u256&gt;
```

## Function peel_enum_tag

Peel enum from serialized bytes, where &#36;f takes a tag value and returns
the corresponding enum variant. Move enums are limited to 127 variants,
however the tag can be any u32 value.

Example:
```rust
let my_enum = match (bcs.peel_enum_tag()) \{
   0 => Enum::Empty,
   1 => Enum::U8(bcs.peel_u8()),
   2 => Enum::U16(bcs.peel_u16()),
   3 => Enum::Struct { a: bcs.peel_address(), b: bcs.peel_u8() \},
   _ => abort,
};
```

```
public fun peel_enum_tag(bcs: &mut sui::bcs::BCS): u32
```

## Macro function peel_option

Peel Option&lt;&#36;T&gt; from serialized bytes, where &#36;peel: |&**mut** [BCS](../sui_sui/bcs#sui_bcs_BCS)| -&gt; &#36;T gives the
functionality of peeling the inner value.

```
public macro fun peel_option&lt;$T&gt;($bcs: &mut sui::bcs::BCS, $peel: |&mut sui::bcs::BCS| -&gt; $T): std::option::Option&lt;$T&gt;
```

## Function peel_option_address

Peel Option&lt;**address**&gt; from serialized bytes.

```
public fun peel_option_address(bcs: &mut sui::bcs::BCS): std::option::Option&lt;address&gt;
```

## Function peel_option_bool

Peel Option&lt;bool&gt; from serialized bytes.

```
public fun peel_option_bool(bcs: &mut sui::bcs::BCS): std::option::Option&lt;bool&gt;
```

## Function peel_option_u8

Peel Option&lt;u8&gt; from serialized bytes.

```
public fun peel_option_u8(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u8&gt;
```

## Function peel_option_u16

Peel Option&lt;u16&gt; from serialized bytes.

```
public fun peel_option_u16(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u16&gt;
```

## Function peel_option_u32

Peel Option&lt;u32&gt; from serialized bytes.

```
public fun peel_option_u32(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u32&gt;
```

## Function peel_option_u64

Peel Option&lt;u64&gt; from serialized bytes.

```
public fun peel_option_u64(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u64&gt;
```

## Function peel_option_u128

Peel Option&lt;u128&gt; from serialized bytes.

```
public fun peel_option_u128(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u128&gt;
```

## Function peel_option_u256

Peel Option&lt;u256&gt; from serialized bytes.

```
public fun peel_option_u256(bcs: &mut sui::bcs::BCS): std::option::Option&lt;u256&gt;
```
