user126956: I was following the tutorial here : https://hackernoon.com/how-to-generate-bitcoin-addresses-technical-address-generation-explanation-rus3z9e
i imported base58 to my python project :
import base58

I tried to convert the private key at the tutorial into the base58 version
privatekey = "a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f"

# expected : C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk
privatekey_base58 = base58.b58encode(privatekey)

But it didn't give me the expected value like in the tutorial , instead it gives me this

How do i properly encode a string to base58 and decode base58 to original string ?

user38618: Use b58encode_check instead of non-check version and convert your hex to binary before passing it to the library.

user134019: You need to convert string hex to bytes hex format before feeding it to b58encode to produce C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk.
import base58
    
privatekey_base58 = base58.b58encode(bytes.fromhex(
    "a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f"
))

The tutorial you linked to is not accessible anymore, so I am not sure whether they talked about it there, but there are actually several steps between generation private key and conversion to Wallet Import Format (WIF). You can see more details about it at https://en.bitcoin.it/wiki/Wallet_import_format. According to the wiki page, you need to extend private key with 0x80 (for mainnet addresses). Extended private key is then passed through double sha256 to extract a checksum (first 4 bytes). Finally, extended private key and checksum are concatenated and Base58 check encoding is applied to produce WIF private key. Surprisingly enough, their tutorial does not use Base58 check encoding, but Base58 encoding.

126956: <p>I was following the tutorial here : <a href="https://hackernoon.com/how-to-generate-bitcoin-addresses-technical-address-generation-explanation-rus3z9e" rel="nofollow noreferrer">https://hackernoon.com/how-to-generate-bitcoin-addresses-technical-address-generation-explanation-rus3z9e</a></p>
<p>i imported <a href="https://pypi.org/project/base58/" rel="nofollow noreferrer">base58</a> to my python project :</p>
<pre><code>import base58
</code></pre>
<p>I tried to convert the private key at the tutorial into the base58 version</p>
<pre><code>privatekey = &quot;a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f&quot;

# expected : C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk
privatekey_base58 = base58.b58encode(privatekey)
</code></pre>
<p>But it didn't give me the expected value like in the tutorial , instead it gives me this
<a href="https://i.stack.imgur.com/z8q56.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z8q56.png" alt="enter image description here" /></a></p>
<p>How do i properly encode a string to base58 and decode base58 to original string ?</p>
 38618: <p>Use b58encode_check instead of non-check version and convert your hex to binary before passing it to the library.</p>
 134019: <p>You need to convert string hex to bytes hex format before feeding it to <code>b58encode</code> to produce <code>C6t2iJ7AXA2X1KQVnqw3r7NKtKaERdLnaGmbDZNqxXjk</code>.</p>
<pre><code>import base58
    
privatekey_base58 = base58.b58encode(bytes.fromhex(
    &quot;a4f228d49910e8ecb53ba6f23f33fbfd2bad442e902ea20b8cf89c473237bf9f&quot;
))
</code></pre>
<p>The tutorial you linked to is not accessible anymore, so I am not sure whether they talked about it there, but there are actually several steps between generation private key and conversion to Wallet Import Format (WIF). You can see more details about it at <a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Wallet_import_format</a>. According to the wiki page, you need to extend private key with <code>0x80</code> (for mainnet addresses). Extended private key is then passed through double <code>sha256</code> to extract a checksum (first 4 bytes). Finally, extended private key and checksum are concatenated and Base58 check encoding is applied to produce WIF private key. Surprisingly enough, their tutorial does not use Base58 check encoding, but Base58 encoding.</p>
