# Text to Binary: What Actually Happens When You Encode a String

Every string you type is a lie the screen tells you. The computer never sees "Hi", it sees `01001000 01101001`. Understanding that translation is one of those small pieces of knowledge that quietly makes you better at debugging encodings, file formats, and network payloads.

Here is the whole idea in three steps.

## Step 1: A character is a number

Text encoding starts with a lookup. Every character has a code point, a plain integer. In ASCII, the letter `A` is 65 and `a` is 97.

| Character | Code | Binary |
| --- | --- | --- |
| A | 65 | 01000001 |
| H | 72 | 01001000 |
| i | 105 | 01101001 |
| space | 32 | 00100000 |

Notice the pattern: uppercase letters start with `010`, lowercase with `011`. That is not a coincidence, it is how the ASCII table was laid out.

## Step 2: The number becomes a byte

A byte is eight bits. So each code point gets written as eight binary digits, padding with leading zeros when needed. This padding is the part people forget.

`H` is 72, which is `1001000` in raw binary. That is only seven bits. Pad it to a full byte and you get `01001000`. Skip the padding and the whole string becomes impossible to split back apart.

## Step 3: Bytes become bits

Line the bytes up and you have your binary string. "Hi" is:

`01001000 01101001`

To read it back, cut it into eight-bit chunks and reverse the lookup.

## Doing it in JavaScript

The safe way uses `TextEncoder`, which gives you real UTF-8 bytes instead of relying on `charCodeAt`.

```js
const textToBinary = (text) =>
  Array.from(new TextEncoder().encode(text))
    .map((b) => b.toString(2).padStart(8, "0"))
    .join(" ");

const binaryToText = (bin) => {
  const bits = bin.replace(/\s+/g, "");
  const bytes = Uint8Array.from(
    { length: bits.length / 8 },
    (_, i) => parseInt(bits.slice(i * 8, i * 8 + 8), 2)
  );
  return new TextDecoder("utf-8").decode(bytes);
};

textToBinary("Hi");                 // "01001000 01101001"
binaryToText("01001000 01101001");  // "Hi"
```

### **The UTF-8 trap**

ASCII fits in one byte, so one character equals eight bits. That comfortable assumption breaks the moment you leave plain English.

`textToBinary("é"); // "11000011 10101001" (two bytes) textToBinary("🚀"); // four bytes, 32 bits`

Accented letters, non-Latin scripts, and emoji all take more than one byte in UTF-8. Any converter built on charCodeAt and a fixed eight-bit assumption will silently corrupt them. TextEncoder and TextDecoder get it right because they speak actual UTF-8.

### A quick way to try it

If you just want to convert something without opening a console, I put this exact logic behind a UI: the [Text to Binary Translator](https://pixellize.io/text-to-binary) on Pixellize. It converts both directions as you type, handles full Unicode, and runs entirely in your browser, so nothing you paste leaves your machine.

### The takeaway

Text to binary is character to code point, code point to byte, byte to eight bits. Pad every byte so the round trip stays reversible, and let UTF-8 tooling handle anything past plain ASCII. Once that clicks, encoding bugs stop feeling like magic and start looking like a checklist.
