TeachMeBitcoin

Base58 Arithmetic

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

Base58 Arithmetic

Converting a hexadecimal number to Base58 is not a simple character swap. It is a full-scale mathematical transformation. To understand how it works, we must treat the entire public key hash as one giant integer.

1. Treat Bytes as a Big Number

A Bitcoin address is usually 25 bytes of data (1 version + 20 hash + 4 checksum).

2. The Algorithm

  1. Take the giant number ($N$).

  2. Divide $N$ by 58.

  3. The Remainder (0 to 57) corresponds to a character in the Base58 alphabet. This becomes the last character of the address.

  4. The Quotient becomes the new $N$.

  5. Repeat until the Quotient is zero.

3. Reverse Order

Because we are finding the remainders from right to left, the characters are generated in reverse order. The first division gives you the very last character of the address.

4. Why Base 58?

The number 58 was chosen because it is roughly $2^6$ (which is 64).

5. Calculation Example

Imagine we are encoding the number 1000 into a custom Base58:

  1. $1000 \div 58 = 17$ (Quotient), Remainder 14.

  2. Character 14 in the alphabet is F.

  3. $17 \div 58 = 0$ (Quotient), Remainder 17.

  4. Character 17 in the alphabet is J.

  5. Result: JF.

Step Operation Resulting Char
1 Large Number % 58 Char at index R
2 Large Number // 58 New Number
3 Repeat Build string backwards

In the next section, we will discuss the special handling of Leading Zeroes.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!