text paste
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>16-Bit Base Converter</title>
  <style>
    body {
      font-family: system-ui, -apple-system, sans-serif;
      background-color: #121212;
      color: #e0e0e0;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
    .container {
      background: #1e1e1e;
      padding: 2rem;
      border-radius: 10px;
      box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
      border: 1px solid #2e2e2e;
      width: 100%;
      max-width: 420px;
    }
    h2 {
      margin-top: 0;
      margin-bottom: 0.25rem;
      color: #ffffff;
      text-align: center;
    }
    .subtitle {
      text-align: center;
      font-size: 0.85rem;
      color: #888;
      margin-bottom: 1.5rem;
    }
    .input-group {
      margin-bottom: 1.25rem;
    }
    label {
      display: flex;
      justify-content: space-between;
      margin-bottom: 0.4rem;
      font-weight: 600;
      font-size: 0.9rem;
      color: #b0b0b0;
    }
    .range-hint {
      font-weight: 400;
      font-size: 0.8rem;
      color: #666;
    }
    input {
      width: 100%;
      padding: 0.7rem;
      font-size: 1.05rem;
      font-family: 'Courier New', Courier, monospace;
      background-color: #2a2a2a;
      color: #ffffff;
      border: 1px solid #3d3d3d;
      border-radius: 6px;
      box-sizing: border-box;
      transition: border-color 0.2s;
    }
    input::placeholder {
      color: #555;
    }
    input:focus {
      outline: none;
      border-color: #4dabf7;
    }
    .error {
      color: #ff6b6b;
      font-size: 0.875rem;
      min-height: 1.2rem;
      text-align: center;
      margin-top: 0.5rem;
    }
  </style>
</head>
<body>

  <div class="container">
    <h2>16-Bit Base Converter</h2>
    <div class="subtitle">Unsigned 16-bit integer (0 – 65535)</div>

    <div class="input-group">
      <label for="dec">Decimal <span class="range-hint">Base 10</span></label>
      <input type="text" id="dec" placeholder="e.g. 65535">
    </div>

    <div class="input-group">
      <label for="hex">Hexadecimal <span class="range-hint">Base 16</span></label>
      <input type="text" id="hex" placeholder="e.g. FFFF">
    </div>

    <div class="input-group">
      <label for="bin">Binary <span class="range-hint">16-bit grouped</span></label>
      <input type="text" id="bin" placeholder="e.g. 1111 1111 1111 1111">
    </div>

    <div id="error-msg" class="error"></div>
  </div>

  <script>
    const decInput = document.getElementById('dec');
    const hexInput = document.getElementById('hex');
    const binInput = document.getElementById('bin');
    const errorMsg = document.getElementById('error-msg');

    const MAX_16BIT = 65535; // 2^16 - 1

    function formatBinary(num) {
      // Pad to 16 bits and split into groups of 4
      return num.toString(2).padStart(16, '0').match(/.{1,4}/g).join(' ');
    }

    function updateFields(source, value) {
      errorMsg.textContent = '';
      // Remove all whitespace so users can type or paste spaced binary
      const cleanVal = value.trim().replace(/\s+/g, '');

      if (cleanVal === '') {
        decInput.value = '';
        hexInput.value = '';
        binInput.value = '';
        return;
      }

      try {
        let num;

        if (source === 'dec') {
          if (!/^\d+$/.test(cleanVal)) throw new Error('Invalid Decimal');
          num = parseInt(cleanVal, 10);
        } 
        else if (source === 'hex') {
          const hexClean = cleanVal.replace(/^0x/i, '');
          if (!/^[0-9a-fA-F]+$/.test(hexClean)) throw new Error('Invalid Hexadecimal');
          num = parseInt(hexClean, 16);
        } 
        else if (source === 'bin') {
          const binClean = cleanVal.replace(/^0b/i, '');
          if (!/^[01]+$/.test(binClean)) throw new Error('Invalid Binary');
          num = parseInt(binClean, 2);
        }

        if (isNaN(num) || num < 0 || num > MAX_16BIT) {
          throw new Error('Value exceeds 16-bit unsigned range (0 – 65535)');
        }

        // Update the other fields
        if (source !== 'dec') decInput.value = num.toString(10);
        if (source !== 'hex') hexInput.value = num.toString(16).toUpperCase().padStart(4, '0');
        if (source !== 'bin') binInput.value = formatBinary(num);

      } catch (err) {
        errorMsg.textContent = err.message;
        if (source !== 'dec') decInput.value = '';
        if (source !== 'hex') hexInput.value = '';
        if (source !== 'bin') binInput.value = '';
      }
    }

    decInput.addEventListener('input', (e) => updateFields('dec', e.target.value));
    hexInput.addEventListener('input', (e) => updateFields('hex', e.target.value));
    binInput.addEventListener('input', (e) => updateFields('bin', e.target.value));
  </script>

</body>
</html>