forked from phishman3579/java-algorithms-implementation
-
Notifications
You must be signed in to change notification settings - Fork 7
JavaScript Convert Decimal to Hex
Ramesh Fadatare edited this page Aug 11, 2020
·
1 revision
In this example, we will write a JavaScript function to convert Decimal number to Hex number.
function intToHex (num) {
switch (num) {
case 10: return 'A'
case 11: return 'B'
case 12: return 'C'
case 13: return 'D'
case 14: return 'E'
case 15: return 'F'
}
return num
}
function decimalToHex (num) {
const hexOut = []
while (num > 15) {
hexOut.unshift(intToHex(num % 16))
num = Math.floor(num / 16)
}
return intToHex(num) + hexOut.join('')
}
// test
console.log(decimalToHex(999098) )
console.log(decimalToHex(123))
Output:
F3EBA
7B