Chuyển đổi số thập phân thành số nhị phân, thập lục phân hoặc bát phân
Ví dụ:
Mã nguồn:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Convert a decimal number to binary, hexadecimal or octal number</title>
<script>
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.........");
}
}
document.write(dec_to_bho(120,'B')+"<br/>");
document.write(dec_to_bho(120,'H')+"<br/>");
document.write(dec_to_bho(120,'O')+"<br/>");
</script>
</head>
<body>
</body>
</html>
Lưu đồ thuật toán:

Thấy bài viết hữu ích?
Ủng hộ tác giả để có thêm nhiều bài viết chất lượng nhé!