Skip to content

Instantly share code, notes, and snippets.

@CodeMaxter
Created June 26, 2018 20:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CodeMaxter/ecf0b7cacccd6b292919537176c0de32 to your computer and use it in GitHub Desktop.
Save CodeMaxter/ecf0b7cacccd6b292919537176c0de32 to your computer and use it in GitHub Desktop.
/**
*
* @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