API reference › @evolu/common › Bytes › hexToBytes

function hexToBytes(hex: string): Uint8Array<ArrayBuffer>;

Defined in: packages/common/src/Bytes.ts:101

Decodes hexadecimal text into bytes.

Accepts lowercase and uppercase digits. Throws a RangeError when the text has an odd length or a character that is not a hexadecimal digit, so validate untrusted text with Hex and decode it with hexToUint8Array.

Example

import { assertEqual, assertThrowsInstanceOf, hexToBytes } from "@evolu/common";

assertEqual(hexToBytes("00CAfe"), new Uint8Array([0, 202, 254]));
assertThrowsInstanceOf(() => hexToBytes("abc"), RangeError);
assertThrowsInstanceOf(() => hexToBytes("0xff"), RangeError);