Skip to content

Instantly share code, notes, and snippets.

@sbp
Created July 3, 2015 13:24
Show Gist options
  • Save sbp/ad9306476e22d040a5fe to your computer and use it in GitHub Desktop.
Save sbp/ad9306476e22d040a5fe to your computer and use it in GitHub Desktop.
Convert bytes to base58
#!/usr/bin/env python3
import sys
base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
def base_encode(octets, alphabet):
base = len(alphabet)
bignum = 0
for (index, octet) in enumerate(reversed(octets)):
bignum += octet * (256 ** index)
output = []
while bignum >= base:
bignum, mod = divmod(bignum, base)
output.append(alphabet[mod])
output.append(alphabet[bignum])
return "".join(reversed(output))
def main():
octets = sys.stdin.buffer.read(1048577)
if len(octets) > 1048576:
sys.stderr.write("Sorry, only input of up to 1 MB is supported")
sys.exit(1)
print(base_encode(octets, base58))
if __name__ == "__main__":
main()
@AvadelRosario
Copy link

Hi. I'm new in the Phyton Programming Language. I was wndering how to run a .py file in my laptop. Hope you could help. Mine's OS is Windows 7 ULtimate. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment