Skip to the content.

Binary Calculator


    <title>Binary to Hexadecimal Converter</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
        }
        input, button {
            padding: 10px;
            margin: 10px 0;
            width: 100%;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        table, th, td {
            border: 1px solid black;
        }
        th, td {
            padding: 10px;
            text-align: left;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Binary to Hexadecimal Converter</h1>
        <input type="text" id="binaryInput" placeholder="Enter binary number" />
        <button onclick="convertBinary()">Convert</button>
        <h2>Result</h2>
        <p id="result"></p>
        <h2>Conversion History</h2>
        <table>
            <thead>
                <tr>
                    <th>Binary</th>
                    <th>Hexadecimal</th>
                </tr>
            </thead>
            <tbody id="history"></tbody>
        </table>
    </div>
    <script>
        function convertBinary() {
            const binaryInput = document.getElementById('binaryInput').value;
            let resultElement = document.getElementById('result');

            if (!/^[01]+$/.test(binaryInput)) {
                resultElement.textContent = "Invalid binary input. Please enter a binary number.";
                return;
            }

            const decimalValue = parseInt(binaryInput, 2);
            const hexadecimalValue = decimalValue.toString(16).toUpperCase();
            resultElement.textContent = `Hexadecimal: ${hexadecimalValue}`;

            saveToHistory(binaryInput, hexadecimalValue);
            loadHistory();
        }

        function saveToHistory(binary, hexadecimal) {
            let history = JSON.parse(localStorage.getItem('conversionHistory')) || [];
            history.push({ binary, hexadecimal });
            localStorage.setItem('conversionHistory', JSON.stringify(history));
        }

        function loadHistory() {
            const historyTable = document.getElementById('history');
            const history = JSON.parse(localStorage.getItem('conversionHistory')) || [];
            historyTable.innerHTML = history.map(entry => `
                <tr>
                    <td>${entry.binary}</td>
                    <td>${entry.hexadecimal}</td>
                </tr>
            `).join('');
        }

        loadHistory();
    </script>
</body>