Created
June 26, 2018 20:12
-
-
Save CodeMaxter/ecf0b7cacccd6b292919537176c0de32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {string} number | |
* @return {number} | |
*/ | |
function hexToDec(number) { | |
// Return error if number is not hexadecimal or contains more than ten characters (10 digits) | |
if (!/^[0-9A-Fa-f]{1,10}$/.test(number)) { | |
return NaN; | |
} | |
// Convert hexadecimal number to decimal | |
const decimal = parseInt(number, 16); | |
// Return decimal number | |
return (decimal >= 549755813888) ? decimal - 1099511627776 : decimal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment