Function hexToNumber

  • Convert a hexadecimal string to a number. This verifies that the string is a valid hex string, and that the resulting number is a safe integer. Both "0x"-prefixed and unprefixed strings are supported.

    To convert a hexadecimal string to a bigint instead, use hexToBigInt.

    Example

    hexToNumber('0x0'); // 0
    hexToNumber('0x1'); // 1
    hexToNumber('0x10'); // 16

    Returns

    The number.

    Throws

    If the value is not a valid hexadecimal string, or if the resulting number is not a safe integer.

    Parameters

    • value: string

      The hexadecimal string to convert to a number.

    Returns number