Number System Converter

Convert numbers between Decimal, Binary, Octal, and Hexadecimal systems.

Decimal
Binary
Octal
Hexadecimal

Decimal
Binary
Octal
Hexadecimal

Result:

function convertNumber() {
const from = document.getElementById(“from”).value;
const to = document.getElementById(“to”).value;
const input = document.getElementById(“inputValue”).value.trim();
const resultField = document.getElementById(“result”);

if (!input) {
resultField.innerText = “”;
return;
}

let baseMap = { decimal: 10, binary: 2, octal: 8, hex: 16 };
const fromBase = baseMap[from];
const toBase = baseMap[to];

try {
const number = parseInt(input, fromBase);
if (isNaN(number)) throw new Error(“Invalid input for selected base.”);

let output = number.toString(toBase);
if (to === “hex”) output = output.toUpperCase();

resultField.innerText = `${from} ${input} → ${to}: ${output}`;
} catch (e) {
resultField.innerText = “Error: ” + e.message;
}
}

function resetConverter() {
document.getElementById(“from”).selectedIndex = 0;
document.getElementById(“to”).selectedIndex = 0;
document.getElementById(“inputValue”).value = “”;
document.getElementById(“result”).innerText = “”;
}


(adsbygoogle = window.adsbygoogle || []).push({});