mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
added DecimalToHex
This commit is contained in:
19
Conversions/DecimalToHex.js
Normal file
19
Conversions/DecimalToHex.js
Normal file
@ -0,0 +1,19 @@
|
||||
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){
|
||||
let hex_out = [];
|
||||
while(num > 15) {
|
||||
hex_out.push(intToHex(num%16));
|
||||
num = Math.floor(num / 16);
|
||||
}
|
||||
return hex_out.join("");
|
||||
}
|
Reference in New Issue
Block a user