How to Make a Text to Binary Converter With HTML and JavaScript

Updated · 6 min read · By the Hacker Typer team

This project builds a small tool: type text in one box and its binary code appears in another, and the same in reverse. It is a good introduction to how computers store letters, and the whole thing is one short file.

The word Hi converted to binary 01001000 01101001 with the JavaScript steps used
Full version: our Text to Binary Converter adds hex, decimal and support for every language. New to binary? Read how to read binary code first.

The complete code

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Text to Binary Converter</title>
  <style>
    body { font-family: Arial, sans-serif; max-width: 700px; margin: 40px auto; padding: 0 16px; }
    textarea { width: 100%; height: 120px; font: 16px monospace; padding: 8px; box-sizing: border-box; }
    label { display: block; margin: 16px 0 6px; font-weight: bold; }
  </style>
</head>
<body>
  <h1>Text to Binary Converter</h1>

  <label for="text">Text</label>
  <textarea id="text" placeholder="Type text here"></textarea>

  <label for="binary">Binary</label>
  <textarea id="binary" placeholder="Or type binary here"></textarea>

  <script>
    const textBox = document.getElementById("text");
    const binaryBox = document.getElementById("binary");

    function textToBinary(text) {
      return text.split("").map(function (letter) {
        return letter.charCodeAt(0).toString(2).padStart(8, "0");
      }).join(" ");
    }

    function binaryToText(binary) {
      return binary.trim().split(/\s+/).map(function (byte) {
        return String.fromCharCode(parseInt(byte, 2));
      }).join("");
    }

    textBox.addEventListener("input", function () {
      binaryBox.value = textToBinary(textBox.value);
    });

    binaryBox.addEventListener("input", function () {
      textBox.value = binaryBox.value.trim() ? binaryToText(binaryBox.value) : "";
    });
  </script>
</body>
</html>

How to use this code

  1. Get the file. Press Download above the code to save it as text-to-binary.html. Or press Copy code, paste it into a plain text editor (Notepad on Windows, TextEdit in plain-text mode on a Mac, or a code editor such as VS Code) and save it as text-to-binary.html. The name must end in .html, not .txt.
  2. Open it. Double-click the file. It opens in your web browser and starts working straight away.
  3. Change it. Open the same file in the text editor, edit something, save, and refresh the browser page to see the result.

Nothing needs to be installed, and the file works without an internet connection.

How the code works

Text to binary, one step at a time

Take the word Hi and follow it through textToBinary:

StepCodeResult
Split into letterstext.split("")"H", "i"
Get each letter's numbercharCodeAt(0)72, 105
Write the number in base 2toString(2)"1001000", "1101001"
Pad to eight digitspadStart(8, "0")"01001000", "01101001"
Join with spacesjoin(" ")"01001000 01101001"

map is what applies the middle three steps to every letter in the list.

Binary back to text

Making it live

addEventListener("input", ...) runs a function every time the contents of a box change. Typing in the text box fills the binary box, and typing in the binary box fills the text box.

Easy changes to try

One limit to know about

This simple version handles English letters, numbers and common symbols correctly. Characters such as emoji and some accented letters need more than eight bits, so they will not convert back properly. Handling every character needs the TextEncoder tool built into browsers, which is what our full converter uses.

FAQ

Why is every letter eight digits?

Eight binary digits make one byte, the standard unit for storing a basic character. Padding with zeros keeps every letter the same length so the code can be split up again.

What does toString(2) do?

It writes a number as text in base 2. The 2 is the base. Using 16 gives hexadecimal and 8 gives octal.

Is my text sent anywhere?

No. The code runs entirely in your browser and works with no internet connection.

More tools

See all tools

Keep reading