Skip to content
On this page

pad โ€‹

Pads a hex value or byte array with leading or trailing zeros.

Install โ€‹

ts
import { pad } from 'viem'

Usage โ€‹

By default, pad will pad a value with leading zeros up to 32 bytes (64 hex chars).

ts
import { pad } from 'viem'

pad('0xa4e12a45')
// 0x00000000000000000000000000000000000000000000000000000000a4e12a45

pad(new Uint8Array([1, 122, 51, 123]))
// Uint8Array [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,122,51,123]

Returns โ€‹

Hex | ByteArray

The value with padded zeros.

Parameters โ€‹

dir โ€‹

  • Type: "left" | "right"
  • Default: "left"

The direction in which to pad the zeros โ€“ either leading (left), or trailing (right).

ts
pad('0xa4e12a45', {
  dir: 'right'
})
// 0xa4e12a4500000000000000000000000000000000000000000000000000000000

size โ€‹

  • Type: number
  • Default: 32

Size (in bytes) of the targeted value.

ts
pad('0xa4e12a45', {
  size: 16
})
// 0x000000000000000000000000a4e12a45

Released under the MIT License.