Hexa to binary converter tool Code for free. Written in HTML, Css and Javacript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Binary to Hex Converter</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<style>
body {
font-family: Arial, sans-serif;
background: linear-gradient(to right, #ff7e5f, #feb47b);
color: #333;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.converter-container {
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
max-width: 400px;
width: 100%;
text-align: center;
}
.converter-container h1 {
margin-bottom: 20px;
color: #ff7e5f;
}
.input-group {
margin-bottom: 15px;
}
.input-group input {
width: 100%;
padding: 10px;
border: 2px solid #ff7e5f;
border-radius: 5px;
outline: none;
font-size: 16px;
}
.buttons {
display: flex;
justify-content: space-between;
}
.buttons button {
background: #ff7e5f;
color: #fff;
border: none;
border-radius: 5px;
padding: 10px 20px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
}
.buttons button:hover {
background: #feb47b;
}
.output {
margin-top: 20px;
padding: 10px;
background: #f9f9f9;
border-radius: 5px;
border: 1px solid #ddd;
word-break: break-word;
}
.output span {
font-weight: bold;
color: #ff7e5f;
}
</style>
</head>
<body>
<div class="converter-container">
<h1>Binary to Hex Converter</h1>
<div class="input-group">
<input type="text" id="binaryInput" placeholder="Enter Binary Number">
</div>
<div class="buttons">
<button id="convertBtn">Convert</button>
<button id="clearBtn">Clear</button>
</div>
<div class="output" id="hexOutput">
<p>Hexadecimal Output: <span id="hexResult">N/A</span></p>
</div>
</div>
<script>
// DOM Elements
const binaryInput = document.getElementById('binaryInput');
const convertBtn = document.getElementById('convertBtn');
const clearBtn = document.getElementById('clearBtn');
const hexResult = document.getElementById('hexResult');
// Convert binary to hexadecimal
function convertBinaryToHex(binary) {
if (!/^[01]+$/.test(binary)) {
alert('Please enter a valid binary number (0s and 1s only).');
return null;
}
return parseInt(binary, 2).toString(16).toUpperCase();
}
// Event listeners
convertBtn.addEventListener('click', () => {
const binary = binaryInput.value.trim();
if (binary) {
const hex = convertBinaryToHex(binary);
if (hex !== null) {
hexResult.textContent = hex;
} else {
hexResult.textContent = 'N/A';
}
} else {
alert('Input cannot be empty.');
}
});
clearBtn.addEventListener('click', () => {
binaryInput.value = '';
hexResult.textContent = 'N/A';
});
// Ensure binary input accepts only valid characters
binaryInput.addEventListener('input', (event) => {
event.target.value = event.target.value.replace(/[^01]/g, '');
});
</script>
</body>
</html>