Binary Encoder/Decoder

Encode text to binary or decode binary back to text


1. What is Binary?

Binary is a base-2 number system that uses only two digits: 0 and 1. In computers, text is stored as binary data where each character is represented by 8 bits (1 byte).

2. How does it work?

Binary encoding converts text characters to their ASCII/Unicode numeric values, then represents each number in base-2 (binary). Each character becomes an 8-bit byte. For example, 'A' has ASCII value 65, which is 01000001 in binary. Decoding reverses this process by converting 8-bit chunks back to characters.

3. Examples

Encode simple text

Hello → 01001000 01100101 01101100 01101100 01101111

Encode single character

A → 01000001

Decode binary

01001000 01101001 → Hi

4. Source Code

typescript
1// Encode text to binary
2function textToBinary(text: string): string {
3  return text
4    .split('')
5    .map(char => {
6      const binary = char.charCodeAt(0).toString(2);
7      return binary.padStart(8, '0');
8    })
9    .join(' ');
10}