> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://v3.viem.sh/api/mcp` to find what you need.

# Bytes

:::info
`Bytes` is a re-export of ox's [`Bytes`](https://oxlib.sh/api/Bytes). Refer to the ox documentation for the full reference.
:::

A set of Ethereum-related utility functions for working with [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) instances.

## Examples

Below are some examples demonstrating common usages of the `Bytes` module:

* [Instantiating Bytes](#instantiating-bytes)

* [Converting from Bytes](#converting-from-bytes)

* [Concatenating Bytes](#concatenating-bytes)

* [Slicing Bytes](#slicing-bytes)

* [Padding Bytes](#padding-bytes)

* [Trimming Bytes](#trimming-bytes)

### Instantiating Bytes

Values can be instantiated as [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) using:

* [`Bytes.fromArray`](/docs/utilities/bytes/fromArray)

* [`Bytes.fromBoolean`](/docs/utilities/bytes/fromBoolean)

* [`Bytes.fromHex`](/docs/utilities/bytes/fromHex)

* [`Bytes.fromNumber`](/docs/utilities/bytes/fromNumber)

* [`Bytes.fromString`](/docs/utilities/bytes/fromString)

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const value_array = Bytes.from([1, 2, 3, 4, 5])
// @log: Uint8Array [1, 2, 3, 4, 5]

const value_boolean = Bytes.fromBoolean(true)
// @log: Uint8Array [1]

const value_hex = Bytes.fromHex('0x1234567890abcdef')
// @log: Uint8Array [18, 52, 86, 120, 144, 175, 207, 15]

const value_number = Bytes.fromNumber(1234567890)
// @log: Uint8Array [4, 160, 216]

const value_string = Bytes.fromString('Hello World!')
// @log: Uint8Array [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
```

### Converting from Bytes

Values can be converted from [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) using:

* [`Bytes.toBigInt`](/docs/utilities/bytes/toBigInt)

* [`Bytes.toBoolean`](/docs/utilities/bytes/toBoolean)

* [`Bytes.toHex`](/docs/utilities/bytes/toHex)

* [`Bytes.toNumber`](/docs/utilities/bytes/toNumber)

* [`Bytes.toString`](/docs/utilities/bytes/toString)

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const value_bigint = Bytes.toBigInt(
  Bytes.from([4, 160, 216])
)
// @log: 1234567890n

const value_boolean = Bytes.toBoolean(Bytes.from([1]))
// @log: true

const value_hex = Bytes.toHex(
  Bytes.from([222, 173, 190, 239])
)
// @log: '0xdeadbeef'

const value_number = Bytes.toNumber(
  Bytes.from([4, 160, 216])
)
// @log: 1234567890

const value_string = Bytes.toString(
  Bytes.from([
    72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33
  ])
)
// @log: 'Hello World!'
```

### Concatenating Bytes

Values can be concatenated using [`Bytes.concat`](/docs/utilities/bytes/concat):

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const a = Bytes.from([1, 2, 3])
const b = Bytes.from([4, 5, 6])
const c = Bytes.concat(a, b)
// @log: Uint8Array [1, 2, 3, 4, 5, 6]
```

### Slicing Bytes

Values can be sliced using [`Bytes.slice`](/docs/utilities/bytes/slice):

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const value = Bytes.slice(
  Bytes.from([1, 2, 3, 4, 5, 6]),
  2,
  4
)
// @log: Uint8Array [3, 4]
```

### Padding Bytes

Values can be padded with zeroes using [`Bytes.padLeft`](/docs/utilities/bytes/padLeft) or [`Bytes.padRight`](/docs/utilities/bytes/padRight):

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const value_1 = Bytes.padLeft(Bytes.from([1, 2, 3]), 5)
// @log: Uint8Array [0, 0, 1, 2, 3]

const value_2 = Bytes.padRight(Bytes.from([1, 2, 3]), 5)
// @log: Uint8Array [1, 2, 3, 0, 0]
```

### Trimming Bytes

Zeroes in values can be trimmed using [`Bytes.trimLeft`](/docs/utilities/bytes/trimLeft) or [`Bytes.trimRight`](/docs/utilities/bytes/trimRight):

```ts twoslash
// @noErrors
import { Bytes } from 'viem/utils'

const value = Bytes.trimLeft(Bytes.from([0, 0, 1, 2, 3]))
// @log: Uint8Array [1, 2, 3]
```

## Functions

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Bytes.assert`](/docs/utilities/bytes/assert) | Asserts if the given value is [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.concat`](/docs/utilities/bytes/concat) | Concatenates two or more [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.from`](/docs/utilities/bytes/from) | Instantiates a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value from a `Uint8Array`, a hex string, or an array of unsigned 8-bit integers. |
| [`Bytes.fromArray`](/docs/utilities/bytes/fromArray) | Converts an array of unsigned 8-bit integers into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.fromBoolean`](/docs/utilities/bytes/fromBoolean) | Encodes a boolean value into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.fromHex`](/docs/utilities/bytes/fromHex) | Encodes a [`Hex.Hex`](/docs/utilities/hex/types#hex) value into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.fromNumber`](/docs/utilities/bytes/fromNumber) | Encodes a number value into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.fromString`](/docs/utilities/bytes/fromString) | Encodes a string into [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |
| [`Bytes.isEqual`](/docs/utilities/bytes/isEqual) | Checks if two [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) values are equal. |
| [`Bytes.padLeft`](/docs/utilities/bytes/padLeft) | Pads a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value to the left with zero bytes until it reaches the given `size` (default: 32 bytes). |
| [`Bytes.padRight`](/docs/utilities/bytes/padRight) | Pads a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value to the right with zero bytes until it reaches the given `size` (default: 32 bytes). |
| [`Bytes.random`](/docs/utilities/bytes/random) | Generates random [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) of the specified length. |
| [`Bytes.size`](/docs/utilities/bytes/size) | Retrieves the size of a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value. |
| [`Bytes.slice`](/docs/utilities/bytes/slice) | Returns a section of a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value given a start/end bytes offset. |
| [`Bytes.toBigInt`](/docs/utilities/bytes/toBigInt) | Decodes a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) into a bigint. |
| [`Bytes.toBoolean`](/docs/utilities/bytes/toBoolean) | Decodes a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) into a boolean. |
| [`Bytes.toHex`](/docs/utilities/bytes/toHex) | Encodes a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value into a [`Hex.Hex`](/docs/utilities/hex/types#hex) value. |
| [`Bytes.toNumber`](/docs/utilities/bytes/toNumber) | Decodes a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) into a number. |
| [`Bytes.toString`](/docs/utilities/bytes/toString) | Decodes a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) into a string. |
| [`Bytes.trimLeft`](/docs/utilities/bytes/trimLeft) | Trims leading zeros from a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value. |
| [`Bytes.trimRight`](/docs/utilities/bytes/trimRight) | Trims trailing zeros from a [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes) value. |
| [`Bytes.validate`](/docs/utilities/bytes/validate) | Checks if the given value is [`Bytes.Bytes`](/docs/utilities/bytes/types#bytes). |

## Errors

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Bytes.InvalidBytesBooleanError`](/docs/utilities/bytes/errors#bytesinvalidbytesbooleanerror) | Thrown when the bytes value cannot be represented as a boolean. |
| [`Bytes.InvalidBytesTypeError`](/docs/utilities/bytes/errors#bytesinvalidbytestypeerror) | Thrown when a value cannot be converted to bytes. |
| [`Bytes.SizeExceedsPaddingSizeError`](/docs/utilities/bytes/errors#bytessizeexceedspaddingsizeerror) | Thrown when a the padding size exceeds the maximum allowed size. |
| [`Bytes.SizeOverflowError`](/docs/utilities/bytes/errors#bytessizeoverflowerror) | Thrown when a size exceeds the maximum allowed size. |
| [`Bytes.SliceOffsetOutOfBoundsError`](/docs/utilities/bytes/errors#bytessliceoffsetoutofboundserror) | Thrown when a slice offset is out-of-bounds. |

## Types

| Name                | Description                         |
| ------------------- | ----------------------------------- |
| [`Bytes.Bytes`](/docs/utilities/bytes/types#bytesbytes) | Root type for a Bytes array. |
