HEX to Octal Converter Tool Code

HEX to Octal Converter Tool Code is 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>HEX to Octal Converter</title>

  <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">

  <style>

    * {

      margin: 0;

      padding: 0;

      box-sizing: border-box;

    }


    body {

      font-family: 'Poppins', sans-serif;

      background: #f1f1f1;

      display: flex;

      justify-content: center;

      align-items: center;

      min-height: 100vh;

      flex-direction: column;

    }


    .container {

      background: #fff;

      border-radius: 10px;

      box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);

      padding: 30px;

      width: 100%;

      max-width: 400px;

    }


    h1 {

      text-align: center;

      color: #6a1b9a;

      font-weight: 600;

      margin-bottom: 20px;

    }


    label {

      font-weight: 600;

      color: #333;

      display: block;

      margin-bottom: 10px;

    }


    input[type="text"] {

      width: 100%;

      padding: 10px;

      margin-bottom: 20px;

      border-radius: 5px;

      border: 1px solid #ccc;

      font-size: 16px;

      color: #333;

    }


    button {

      width: 100%;

      padding: 10px;

      background-color: #6a1b9a;

      color: #fff;

      font-size: 16px;

      border: none;

      border-radius: 5px;

      cursor: pointer;

      font-weight: 600;

    }


    button:hover {

      background-color: #8e24aa;

    }


    .result {

      margin-top: 20px;

      font-size: 18px;

      text-align: center;

      color: #4caf50;

      font-weight: 600;

    }


    .error {

      color: #e53935;

    }


    /* Responsive Design */

    @media (max-width: 600px) {

      .container {

        padding: 20px;

        width: 100%;

        max-width: 90%;

      }


      h1 {

        font-size: 24px;

      }

    }


  </style>

</head>

<body>

  <div class="container">

    <h1>HEX to Octal Converter</h1>

    <label for="hexInput">Enter HEX Value (e.g., #ff5733):</label>

    <input type="text" id="hexInput" placeholder="Enter HEX value">

    <button onclick="convertToOctal()">Convert to Octal</button>

    <div id="result" class="result"></div>

  </div>


  <script>

    function convertToOctal() {

      const hexInput = document.getElementById('hexInput').value.trim();

      const resultElement = document.getElementById('result');


      // Check if the input is a valid HEX value

      if (/^#[0-9A-Fa-f]{6}$/.test(hexInput)) {

        const hexValue = hexInput.slice(1);  // Remove the '#' symbol

        const decimalValue = parseInt(hexValue, 16);  // Convert HEX to Decimal

        const octalValue = decimalValue.toString(8);  // Convert Decimal to Octal

        resultElement.textContent = `Octal: ${octalValue}`;

        resultElement.classList.remove('error');

      } else {

        resultElement.textContent = 'Please enter a valid HEX value (e.g., #ff5733).';

        resultElement.classList.add('error');

      }

    }

  </script>

</body>

</html>


Admin

A Software Engineer, Social Media Marketing Expert, writer,

Post a Comment

Previous Post Next Post