Skip to content

hexToBytes

hexToBytes(hex_, opts?): ByteArray

Encodes a hex string into a byte array.

Parameters

hex_: `0x${string}`

opts?: HexToBytesOpts

Options.

Returns

ByteArray

Byte array value.

Examples

import { hexToBytes } from 'viem'
const data = hexToBytes('0x48656c6c6f20776f726c6421')
// Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
import { hexToBytes } from 'viem'
const data = hexToBytes('0x48656c6c6f20776f726c6421', { size: 32 })
// Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Defined in

node_modules/.pnpm/viem@2.14.2_bufferutil@4.0.8_typescript@5.5.2_utf-8-validate@6.0.4_zod@3.23.8/node_modules/viem/_types/utils/encoding/toBytes.d.ts:87