API reference › @evolu/common › Type › Uint8ArrayFromHex

const Uint8ArrayFromHex: transform(
  "Uint8ArrayFromHex",
  Hex,
  Uint8Array,
  {
    from: (value) => ok(hexToUint8Array(value)),
    to: uint8ArrayToHex,
  },
);

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

Transforms Hex text into bytes.

Use it to decode binary fields of JSON and other text payloads, for example inside an object. Decoding cannot fail, because Hex accepts only lowercase digits of whole bytes. Encoding returns lowercase text, so equal bytes encode to equal strings.

Example

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

const bytes = Uint8ArrayFromHex.fromUnknown("000102ff");
assertOk(bytes, new Uint8Array([0, 1, 2, 255]));
assertEqual(Uint8ArrayFromHex.to(bytes.value), "000102ff");

assertErr(Uint8ArrayFromHex.fromUnknown("000102FF"), {
  type: "Hex",
  value: "000102FF",
});