Skip to content

otplib API Documentation / otplib / hexToBytes

Function: hexToBytes()

hexToBytes(hex): Uint8Array

Defined in: core/src/utils.ts:587

Convert a hex string to a Uint8Array

This is useful for working with RFC test vectors and debugging HMAC outputs, which are commonly represented as hexadecimal strings.

If your environment supports it, consider using Uint8Array.fromHex() instead.

Parameters

hex

string

The hex string to convert (lowercase or uppercase, no 0x prefix)

Returns

Uint8Array

The bytes as a Uint8Array

Example

ts
import { hexToBytes } from '@otplib/core'

// Convert RFC 4226 HMAC test vector
const hmac = hexToBytes('cc93cf18508d94934c64b65d8ba7667fb7cde4b0')
// Returns: Uint8Array([0xcc, 0x93, 0xcf, ...])

Released under the MIT License.