Created
November 25, 2020 12:25
-
-
Save oscarDev1190/761915a9fbd4a5dd7a80cd347a61712c to your computer and use it in GitHub Desktop.
Encode y Decoder de base10 a 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 python | |
# -*- coding: utf-8 -*- | |
BASE_58 = ('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', | |
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', | |
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z') | |
def div58(n): | |
yield n | |
while True: | |
n = n // 58 | |
if n: | |
yield n | |
else: | |
return | |
def decimal_to_base58(number): | |
res = [] | |
for n in div58(number): | |
res.append(n % 58) | |
return ''.join(BASE_58[i] for i in res[::-1]) | |
def base58_to_decimal(number): | |
return sum( BASE_58.index(v) * (58 ** i) for i, v in enumerate(number[::-1]) ) | |
bitcoin_address_int = 35873778057646637307440121179298518192096716765629162297729 | |
bitcoin_address_b58 = decimal_to_base58(bitcoin_address_int) | |
print('La direccion bitcoin en base 58 es %s' %bitcoin_address_b58) | |
print('En su estado decimal es %s' %base58_to_decimal(bitcoin_address_b58)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment