<!DOCTYPE html>
<html> <head> <title>Hexadecimal to Octal Converter</title> <style> body { font-family: Arial, sans-serif; background-color: #f2f2f2; padding: 20px; } .container { max-width: 800px; margin: 0 auto; } h1 { color: #333; margin-bottom: 20px; } .input-field { width: 100%; padding: 10px; border: 2px solid #ccc; border-radius: 4px; box-sizing: border-box; } .convert-button { margin-top: 10px; padding: 10px 20px; font-size: 16px; font-weight: bold; color: #fff; background-color: #4CAF50; border: none; border-radius: 4px; cursor: pointer; } .convert-button:hover { background-color: #45a049; } .result { margin-top: 20px; border: 2px solid #ccc; border-radius: 4px; padding: 10px; background-color: #fff; } .error { color: #ff0000; } </style> </head> <body> <div class="container"> <h1>Hexadecimal to Octal Converter</h1> <input id="inputHex" class="input-field" type="text" placeholder="Enter hexadecimal number"> <button onclick="convertToOctal()" class="convert-button">Convert</button> <div id="result" class="result"></div> </div> <script> function convertToOctal() { var inputHex = document.getElementById("inputHex").value; var hex = inputHex.trim(); var octal = ""; var isValid = /^[0-9A-Fa-f]+$/.test(hex); if (isValid) { var decimal = parseInt(hex, 16); octal = decimal.toString(8); document.getElementById("result").textContent = "Octal representation: " + octal; } else { document.getElementById("result").innerHTML = '<span class="error">Invalid hexadecimal number</span>'; } } </script> </body> </html>
No comments:
Post a Comment