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

Byte array value.

Example

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

Example

import { toBytes } from 'viem'
const data = toBytes(420)
// Uint8Array([1, 164])

Example

import { toBytes } from 'viem'
const data = toBytes(420, { size: 4 })
// Uint8Array([0, 0, 1, 164])

Source

node_modules/.pnpm/viem@2.4.0_typescript@5.3.3_zod@3.22.4/node_modules/viem/_types/utils/encoding/toBytes.d.ts:37


Generated using typedoc-plugin-markdown and TypeDoc