Represents a hexadecimal value expressed as

  • - sign if the value is negative,
  • 0x hexadecimal notation tag,
  • a not empty string of hexadecimal digits from 0 to 9 and from a to f.

This hexadecimal notation is coherent with the decimal notation:

  • the sign is only expressed for negative values, and it is always the first symbol,
  • the 0x tags the string as a hexadecimal expression,
  • hexadecimal digits follow.
  • An empty content results is no digits.

Hierarchy (view full)

Implements

Constructors

  • Creates a new instance of this class to represent the value built multiplying sign for the absolute value expressed by the hexadecimal digits.

    Parameters

    • sign: number

      The sign of the value.

    • digits: string

      The digits of the absolute value in hexadecimal base.

    • Optionalnormalize: ((digits: string) => string) = ...

      The function used to normalize the digits. Defaults to converting digits to lowercase.

        • (digits): string
        • Parameters

          • digits: string

          Returns string

    Returns Hex

Properties

digits: string

Returns the hexadecimal digits expressing this absolute value, sign and 0x prefix omitted.

An empty content results in an empty string returned.

sign: number

Represents the sign multiplier of a given number:

NEGATIVE: number = -1

Negative multiplier of the digits absolute value.

POSITIVE: number = 1

Positive multiplier of the digits absolute value.

PREFIX: "0x" = '0x'

A constant string prefix used in hexadecimal notation.

RADIX: number = 16

The radix used for representing numbers base 16 in a positional numeral notation system.

REGEX_HEX_PREFIX: RegExp = ...

Regular expression pattern to match a prefix indicating hexadecimal number.

Accessors

  • get bi(): bigint
  • Returns the value of bi as a BigInt type.

    Returns bigint

    The value of bi as a BigInt.

  • get bytes(): Uint8Array
  • Returns the Uint8Array representation of the aligned bytes.

    Returns Uint8Array

    The Uint8Array representation of the aligned bytes.

Methods

  • Aligns the hexadecimal string to bytes by adding a leading '0' if the string length is odd.

    Returns Hex

    • The aligned hexadecimal string.
  • Compares the current Hex object with another Hex object.

    Parameters

    • that: Hex

      The Hex object to compare with.

    Returns number

    • Returns a negative number if the current Hex object is less than the given Hex object, zero if they are equal, or a positive number if the current Hex object is greater than the given Hex object.
  • Returns a new instance of the Hex class, its value fits to the specified number of digits.

    Parameters

    • digits: number

      The number of digits to fit the Hex value into.

    Returns Hex

    • A new Hex instance that represents the fitted Hex value.
    • If the Hex value cannot be fit into the specified number of digits.
  • Determines whether this Hex instance is equal to the given Hex instance.

    Parameters

    • that: Hex

      The Hex instance to compare with.

    Returns boolean

    • True if the Hex instances are equal, otherwise false.
  • Returns a string representation of the object.

    Parameters

    • compact: boolean = false

      Whether to compact the string representation.

    Returns string

    The string representation of the object.

  • Checks if the given string expression is a valid hexadecimal value.

    Parameters

    • exp: string

      The string representation of a hexadecimal value.

    Returns boolean

    • True if the expression is a valid hexadecimal value, case-insensitive, optionally prefixed with 0x; false otherwise.
  • Determines whether the given string is a valid hexadecimal number prefixed with '0x'.

    Parameters

    • exp: string

      The string to be evaluated.

    Returns boolean

    • True if the string is a valid hexadecimal number prefixed with '0x', otherwise false.
  • Create a Hex instance from a bigint, number, string, or Uint8Array.

    Parameters

    • exp:
          | string
          | number
          | bigint
          | Uint8Array

      The value to represent in a Hex instance:

      • bigint is always representable in hexadecimal base notation;
      • number, encoded as IEEE 754 double precision 64 bits floating point format;
      • string is parsed as the hexadecimal expression of a bigint value, optionally tagged with 0x;
      • Uint8Array is interpreted as the sequence of bytes.

    Returns Hex

    • A Hex instance representing the input value.

    if the given exp can't be represented as a hexadecimal expression.

  • Generates a random Hex value of the given number of bytes length.

    Parameters

    • bytes: number

      The number of bytes to generate.

    Returns Hex

    • A randomly generated Hex value.
    • If the bytes argument is not greater than 0.

    Security auditable method, depends on