Skip to content

Instantly share code, notes, and snippets.

@stackdumper
Created May 21, 2018 07:29
Show Gist options
  • Save stackdumper/1163a41d95e9713db72fbbf7e312d343 to your computer and use it in GitHub Desktop.
Save stackdumper/1163a41d95e9713db72fbbf7e312d343 to your computer and use it in GitHub Desktop.
hex2dec
const hex = process.argv[2];
const getInt = char =>
({
A: '10',
B: '11',
C: '12',
D: '13',
E: '14',
F: '15'
}[char.toUpperCase()] || char);
const dec = hex
.split('')
.reverse()
.reduce((acc, char, i) => {
return acc + getInt(hex[i]) * Math.pow(16, hex.length - i - 1);
}, 0);
console.log(dec);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment