(hexString: string)
| 64 | * @throws InvalidParameterError If the input is not a hexadecimal string or the value exceeds the Number.MAX_SAFE_INTEGER limit. |
| 65 | */ |
| 66 | export 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. |
no test coverage detected