Skip to content

otplib API Documentation / @otplib/core / stringToBytes

Function: stringToBytes()

stringToBytes(value): Uint8Array

Defined in: packages/core/src/utils.ts:563

Convert a string or Uint8Array to Uint8Array

This utility function normalizes input to Uint8Array, converting strings using UTF-8 encoding. Uint8Array inputs are returned as-is.

Parameters

value

The value to convert (string or Uint8Array)

string | Uint8Array<ArrayBufferLike>

Returns

Uint8Array

The value as a Uint8Array (UTF-8 encoded for strings)

Example

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

const bytes1 = stringToBytes('1234567890123456')
// Returns: Uint8Array([49, 50, 51, 52, 53, 54, 55, 56, 57, 48, 49, 50, 51, 52, 53, 54])

const bytes2 = stringToBytes(new Uint8Array([1, 2, 3]))
// Returns: Uint8Array([1, 2, 3]) - returned as-is

Released under the MIT License.