Binary to Octal Converter Tool Code written in HTML, JavaScript and Css.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Binary to Octal Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
background-color: #f4f4f9;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.container {
background: #ffffff;
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.1);
padding: 30px;
border-radius: 10px;
width: 100%;
max-width: 400px;
text-align: center;
}
h1 {
color: #5e5e5e;
}
input[type="text"] {
width: 80%;
padding: 10px;
margin: 10px 0;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 16px;
outline: none;
transition: border-color 0.3s;
}
input[type="text"]:focus {
border-color: #3498db;
}
button {
background-color: #3498db;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
font-size: 18px;
cursor: pointer;
width: 100%;
margin-top: 10px;
transition: background-color 0.3s;
}
button:hover {
background-color: #2980b9;
}
.output {
margin-top: 20px;
padding: 10px;
background-color: #e8f4f8;
border-radius: 5px;
color: #333;
font-size: 18px;
word-wrap: break-word;
border: 1px solid #ccc;
}
.error {
color: red;
font-size: 16px;
margin-top: 10px;
}
@media (max-width: 600px) {
.container {
padding: 20px;
}
h1 {
font-size: 24px;
}
input[type="text"] {
width: 90%;
}
button {
font-size: 16px;
}
}
</style>
</head>
<body>
<div class="container">
<h1>Binary to Octal Converter</h1>
<input type="text" id="binaryInput" placeholder="Enter binary number">
<button onclick="convertBinaryToOctal()">Convert</button>
<div id="result" class="output"></div>
<div id="error" class="error"></div>
</div>
<script>
function convertBinaryToOctal() {
const binaryInput = document.getElementById('binaryInput').value;
const errorDiv = document.getElementById('error');
const resultDiv = document.getElementById('result');
errorDiv.innerHTML = '';
resultDiv.innerHTML = '';
// Check if the input is a valid binary number
if (!/^[01]+$/.test(binaryInput)) {
errorDiv.innerHTML = 'Please enter a valid binary number.';
return;
}
// Convert binary to octal
const decimal = parseInt(binaryInput, 2);
const octal = decimal.toString(8);
resultDiv.innerHTML = `Octal: ${octal}`;
}
</script>
</body>
</html>