programming-examples/js/Math/Convert a decimal number to binary, hexadecimal or octal number.js

24 lines
505 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
dec_to_bho = function(n, base) {
if (n < 0) {
n = 0xFFFFFFFF + n + 1;
}
switch (base)
{
case 'B':
return parseInt(n, 10).toString(2);
break;
case 'H':
return parseInt(n, 10).toString(16);
break;
case 'O':
return parseInt(n, 10).toString(8);
break;
default:
return("Wrong input.........");
}
}
console.log(dec_to_bho(120,'B'));
console.log(dec_to_bho(120,'H'));
console.log(dec_to_bho(120,'O'));