Skip to content
On this page

toHex โ€‹

Encodes a string, number, boolean or byte array to a hex value value.

Shortcut Functions:

Import โ€‹

ts
import { toHex } from 'viem'

Usage โ€‹

ts
import { toHex } from 'viem'

toHex(420)
// "0x1a4"

toHex('Hello world')
// "0x48656c6c6f20776f726c642e"

toHex(
  new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33])
)
// "0x48656c6c6f20576f726c6421"

toHex(true)
// "0x1"

Returns โ€‹

Hex

The hex value.

Parameters โ€‹

value โ€‹

  • Type: string | number | bigint | ByteArray

The value to hex encode.

ts
toHex(
  'Hello world'
)
// '0x48656c6c6f20776f726c642e'

options โ€‹

ts
toHex(
  'Hello world', 
  { size: 32 }
)
// '0x48656c6c6f20776f726c642e0000000000000000000000000000000000000000'

Shortcut Functions โ€‹

numberToHex โ€‹

  • Type: number | bigint

Encodes a number value to a hex value.

ts
import { numberToHex } from 'viem'

numberToHex(420)
// "0x1a4"

numberToHex(4206942069420694206942069420694206942069n)
// "0xc5cf39211876fb5e5884327fa56fc0b75"

numberToHex(420, { size: 32 })
// "0x00000000000000000000000000000000000000000000000000000000000001a4"

numberToHex(4206942069420694206942069420694206942069n, { size: 32 })
// "0x0000000000000000000000000000000c5cf39211876fb5e5884327fa56fc0b75"

stringToHex โ€‹

  • Type: string

Encodes a UTF-8 string value to a hex value.

ts
import { stringToHex } from 'viem'

stringToHex('Hello World!')
// "0x48656c6c6f20576f726c6421"

stringToHex('Hello World!', { size: 32 })
// "0x48656c6c6f20576f726c64210000000000000000000000000000000000000000"

bytesToHex โ€‹

  • Type: ByteArray

Encodes a byte array to a hex value.

ts
import { stringToHex } from 'viem'

bytesToHex(
  new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
)
// "0x48656c6c6f20576f726c6421"

bytesToHex(
  new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]),
  { size: 32 }
)
// "0x48656c6c6f20576f726c64210000000000000000000000000000000000000000"

boolToHex โ€‹

  • Type: boolean

Encodes a boolean to a hex value.

ts
import { stringToHex } from 'viem'

bytesToHex(true)
// "0x1"

bytesToHex(true, { size: 32 })
// "0x0000000000000000000000000000000000000000000000000000000000000001"

Released under the MIT License.