Hexadecimal to interger and then to base58 encoding convertion

Please help, the conversion from hexadecimal to integer by calculating below code using formula decimal_integer = int(hexadecimal_string, 16) , the input “0x0284027d82814d44f95c6abe525ca1cfe6d25e38f428f4f7ccce66f6f146a91abb” should produce the output “14694275965585203580140370469025894397667795244428387667290999565940543719152”


def main():

    # Convert the hexadecimal string to an integer
    value = int("0x0284027d82814d44f95c6abe525ca1cfe6d25e38f428f4f7ccce66f6f146a91abb", 16)

    # Print the result
    print(value)

if __name__ == "__main__":
    main()

Then the output integer “14694275965585203580140370469025894397667795244428387667290999565940543719152” should give base58 output of 7Da1bMMDqUXQkwM6Apy1T3Kcm8GSUttogJUqqSiLtQrd using code below;

def modulo(value, base_count):
    return value % (base_count - 1)

def division(value, base_count):
    return value // (base_count - 1)

def encode_base58(value):
    alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    base_count = len(alphabet)
    encoded = ''

    while value >= base_count:
        div, mod = divmod(value, base_count)
        encoded = alphabet[mod] + encoded
        value = div

    if value > 0:
        encoded = alphabet[value] + encoded

    return encoded


def main():
    value = 14694275965585203580140370469025894397667795244428387667290999565940543719152
    encoded_value = encode_base58(value)
    print(encoded_value)
    print(modulo(value, len(encoded_value)))
    print(division(value, len(encoded_value)))


if __name__ == "__main__":
    main()

Please help by reviewing the code and modify so that it gives intended outputs
please help , zibwazibwa@gmail.com

Have you considered using the base58 package on PyPI instead of trying to roll your own implementation?

@CAM-Gerlach Thank you let me go through that

I get this:

>>> int("0x0284027d82814d44f95c6abe525ca1cfe6d25e38f428f4f7ccce66f6f146a91abb", 16)
291293874418456598856160237858566340378761672083743889537713412789409403181755

Thanks Mathews, indeed its giving that when i run it now

Thanks Mathews, indeed when i run it now its giving me that

Am grateful, will the integer produced when run using below code produce the intended base 58 encoded value of Encoded Value: 7Da1bMMDqUXQkwM6Apy1T3Kcm8GSUttogJUqqSiLtQrd
then?

def encode_base58(value):
alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
base_count = len(alphabet)
encoded = ''

if value < 0:
value = -value

while value >= base_count:
div, mod = divmod(value, base_count)
encoded = alphabet[mod] + encoded
value = div

if value > 0:
encoded = alphabet[value] + encoded

return encoded

def main():
value = -291293874418456598856160237858566340378761672083743889537713412789409403181755
encoded_value = encode_base58(value)
print("Encoded Value:", encoded_value)

if __name__ == "__main__":
main()

Thanks Mathews, indeed its giving that when i run it now, so would that output when negated give the intended base58 encoded value of 7Da1bMMDqUXQkwM6Apy1T3Kcm8GSUttogJUqqSiLtQrd when run using the second code or code below;

def encode_base58(value):
    alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'
    base_count = len(alphabet)
    encoded = ''

    if value < 0:
        value = -value

    while value >= base_count:
        div, mod = divmod(value, base_count)
        encoded = alphabet[mod] + encoded
        value = div

    if value > 0:
        encoded = alphabet[value] + encoded

    return encoded


def main():
    value = -291293874418456598856160237858566340378761672083743889537713412789409403181755
    encoded_value = encode_base58(value)
    print("Encoded Value:", encoded_value)


if __name__ == "__main__":
    main()

Your new function negates any negative value you give it, so giving it, say, -1 will have the same result as giving it 1.

As a quick check, look at the base-58 number that you want. It ends with ‘d’, which has the value of 42, which means that value % 58 needs to be 42.

BTW, @TheMusk , please don’t forget to enclose your code in code blocks and include the correct indents, or your code will be nearly unreadable, as you could see in the resulting message (I’ve been editing your posts to do it for you so far). You can easily do so which you can do with the </> button on the toolbar or by copy/pasting this block. I’ve been editing your posts for you to include them. You can check in the preview shown next to your message whether you’ve done it right.

```python
YOUR CODE HERE
```