Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Base 58 Encode
# From
# The description on that page is misleading or just wrong.
# This was derived from converting the Rust code here:
TABLE = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
FORWARD = {k:v for (k, v) in enumerate(TABLE)}
BACK = {k:v for (v, k) in enumerate(TABLE)}
def b58_encode(data):
size = 0
out = []
first = True
for c in data:
acc = ord(c)
i = 0
while acc != 0 or i < size:
if first:
first = False
if i < size:
acc += out[i] * 256
acc, out[i] = divmod(acc, 58)
i += 1
size = i
return ''.join(FORWARD[b] for b in out)
print(b58_encode("Hello World!"))
print(b58_encode("The quick brown fox jumps over the lazy dog."))
# my initial wrong answer
# oGU3JqabjWkQe3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment