Created
July 3, 2015 13:24
-
-
Save sbp/ad9306476e22d040a5fe to your computer and use it in GitHub Desktop.
Convert bytes to base58
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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