Skip to content

toBytes

toBytes(value, opts?): ByteArray

Encodes a UTF-8 string, hex value, bigint, number or boolean to a byte array.

Parameters

value: string | number | bigint | boolean

Value to encode.

opts?: ToBytesParameters

Options.

Returns

ByteArray

Byte array value.

Examples

import { toBytes } from 'viem'
const data = toBytes('Hello world')
// Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
import { toBytes } from 'viem'
const data = toBytes(420)
// Uint8Array([1, 164])
import { toBytes } from 'viem'
const data = toBytes(420, { size: 4 })
// Uint8Array([0, 0, 1, 164])

Source

node_modules/.pnpm/viem@2.13.6_bufferutil@4.0.8_typescript@5.4.5_utf-8-validate@6.0.4_zod@3.23.8/node_modules/viem/_types/utils/encoding/toBytes.d.ts:37