toHex โ
Encodes a string, number, boolean or byte array to a hex value value.
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 โ
The hex value.
Parameters โ
Value โ
- Type:
string | number | bigint | ByteArray
The value to hex encode.
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"
stringToHex โ
- Type:
string
Encodes a UTF-8 string value to a hex value.
ts
import { stringToHex } from 'viem'
stringToHex('Hello World!')
// "0x48656c6c6f20576f726c6421"
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"
boolToHex โ
- Type:
boolean
Encodes a boolean to a hex value.
ts
import { stringToHex } from 'viem'
bytesToHex(true)
// "0x1"