MCPcopy Create free account
hub / github.com/NomicFoundation/hardhat / hexStringToNumber

Function hexStringToNumber

packages/hardhat-utils/src/hex.ts:66–89  ·  view source on GitHub ↗
(hexString: string)

Source from the content-addressed store, hash-verified

64 * @throws InvalidParameterError If the input is not a hexadecimal string or the value exceeds the Number.MAX_SAFE_INTEGER limit.
65 */
66export function hexStringToNumber(hexString: string): number {
67 if (!isHexString(hexString)) {
68 throw new InvalidParameterError(
69 `Expected a valid hexadecimal string. Received: ${hexString}`,
70 );
71 }
72
73 // Prefix the string as it is required to make parseInt interpret it as a
74 // hexadecimal number.
75 let prefixedHexString = getPrefixedHexString(hexString);
76
77 // Handle the special case where the string is "0x".
78 prefixedHexString = prefixedHexString === "0x" ? "0x0" : prefixedHexString;
79
80 const numberValue = parseInt(prefixedHexString, 16);
81
82 if (numberValue > Number.MAX_SAFE_INTEGER) {
83 throw new InvalidParameterError(
84 `Value exceeds the safe integer limit. Received: ${hexString}`,
85 );
86 }
87
88 return numberValue;
89}
90
91/**
92 * Converts a Uint8Array to a hexadecimal string.

Callers 7

#ethSendTransactionMethod · 0.90
#getBlockGasLimitFunction · 0.90
hex.tsFile · 0.85
bytesToNumberFunction · 0.85

Calls 2

isHexStringFunction · 0.85
getPrefixedHexStringFunction · 0.85

Tested by

no test coverage detected