# Binary to text encoding

Operation for commonly used base64, base32, base16, hex, and binary encoding schemes.

## Introduction

### Base 2, Binary (bin)

Digital computers are based on bits, many bits, where each of them has two possible states 1 and 0. A string of bits is also called a binary string (e.g. 01100011). Based on the context we may interpret a group of bits as an integer, a float, a character, or a string of characters.

### Base 16, Hexadecimal (hex)

When we group together 8 bits to a byte, we get $2^8=256$ possible states for this 8-bit group. We have a number system for 10 states at hand (0-9). How can we apply this to 256 states? First, we introduce a number system for 16 states (0-9, A-F) and call it “hexadecimal”. And now here’s the magic trick: We turn each byte into a combination of two groups of 4 bits each, getting $256=2^4\cdot2^4$ possible states and you guessed it, $2^4=16$ which is exactly the number of states we can express using hexadecimal digits. Thus we can express every byte (or group of 8 bits) by 2 consecutive hexadecimal digits (e.g. “00” for decimal 0 and “FF” for 255).

### Base 32 & Base 64

You may ask, we have more than 16 characters that we can differentiate from each other (e.g. G-Z or punctuation symbols), why not make good use of them? Indeed, that’s where base32 and base64 come into play. The number 32 in “base32” denotes the number of states we want to differentiate with each digit. With 32 different states, we can express $\log_2{32}=5$ bits with a single digit. The same goes for 64 states where we get $\log_264=6$ bits. Hence, we can store more information in fewer characters.