Skip to content

Instantly share code, notes, and snippets.

@stackdumper
Last active May 21, 2018 07:29
Show Gist options
  • Save stackdumper/ccf6824099371340e739504aa7504125 to your computer and use it in GitHub Desktop.
Save stackdumper/ccf6824099371340e739504aa7504125 to your computer and use it in GitHub Desktop.
dec2hex
let dec = process.argv[2];
let result = '';
const getChar = char =>
({
'10': 'A',
'11': 'B',
'12': 'C',
'13': 'D',
'14': 'E',
'15': 'F'
}[char] || char);
do {
const float = dec / 16;
const int = (dec = Math.floor(float));
const remainder = (float - int) * 16;
result = getChar(remainder) + result;
} while (dec !== 0);
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment