API reference › @evolu/common › Type › Hex

const Hex: brand(
  "Hex",
  String,
  (value: string) =>
    value.length % 2 === 0 && /^[0-9a-f]*$/u.test(value)
      ? ok()
      : err<HexError>({ type: "Hex", value }),
  formatHexError,
);

Defined in: packages/common/src/Type.ts:8938

Lowercase hexadecimal text of whole bytes.

Every byte is two digits, so the length is even, and length constraints count characters, two per byte. An empty string encodes no bytes. Uppercase digits, prefixes such as 0x, and whitespace are rejected so that equal bytes are equal strings; lowercase text from other sources before validating it.

Convert bytes to Hex with uint8ArrayToHex and convert Hex to bytes with hexToUint8Array. The bytesToHex and hexToBytes functions convert the same text without a brand, and hexToBytes throws on invalid text instead of returning an error.

Example

import { assertErr, assertOk, Hex } from "@evolu/common";

assertOk(Hex.fromUnknown("deadbeef"), "deadbeef");
assertOk(Hex.fromUnknown(""), "");
assertErr(Hex.fromUnknown("DEADBEEF"));
assertErr(Hex.fromUnknown("abc"));
assertErr(Hex.fromUnknown("0xff"));