Complete working Hexa to Binary converter Tool Code written in Javascript HTML and Css.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HEX to Binary Converter Tool</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(to right, #1e3c72, #2a5298);
color: #fff;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
padding: 30px;
border-radius: 15px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
width: 90%;
max-width: 400px;
}
h1 {
font-size: 1.8rem;
margin-bottom: 20px;
}
.input-group {
margin: 20px 0;
}
input {
width: 100%;
padding: 10px;
border: none;
border-radius: 5px;
font-size: 1rem;
}
button {
background-color: #4CAF50;
border: none;
padding: 10px 20px;
color: white;
font-size: 1rem;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #45a049;
}
.output {
margin-top: 20px;
padding: 10px;
background: rgba(0, 0, 0, 0.2);
border-radius: 5px;
word-wrap: break-word;
}
.error {
color: #ff8080;
}
@media (max-width: 768px) {
h1 {
font-size: 1.5rem;
}
button {
font-size: 0.9rem;
}
input {
font-size: 0.9rem;
}
}
</style>
</head>
<body>
<div class="container">
<h1>HEX to Binary Converter</h1>
<div class="input-group">
<input type="text" id="hexInput" placeholder="Enter HEX value">
</div>
<button id="convertBtn">Convert</button>
<div class="output" id="binaryOutput"></div>
</div>
<script>
const hexInput = document.getElementById('hexInput');
const convertBtn = document.getElementById('convertBtn');
const binaryOutput = document.getElementById('binaryOutput');
function isValidHex(hex) {
return /^[0-9a-fA-F]+$/.test(hex);
}
function hexToBinary(hex) {
return hex
.split('')
.map(char => parseInt(char, 16).toString(2).padStart(4, '0'))
.join('');
}
convertBtn.addEventListener('click', () => {
const hexValue = hexInput.value.trim();
if (!hexValue) {
binaryOutput.innerHTML = '<span class="error">Please enter a HEX value!</span>';
return;
}
if (!isValidHex(hexValue)) {
binaryOutput.innerHTML = '<span class="error">Invalid HEX value! Please use 0-9 and A-F.</span>';
return;
}
const binaryValue = hexToBinary(hexValue);
binaryOutput.textContent = `Binary: ${binaryValue}`;
});
</script>
</body>
</html>