<p>Each hexadecimal character has four bits of information. Two hex characters contains eight bits of information, so they form a byte.</p>

<p>For each byte in front of the address, a <code>1</code> should be put. Since two characters form a byte:</p>

<pre><code>int i=0;
while(concat.charAt(i) == '0' &amp;&amp; concat.charAt(i + 1) == '0'){
    i += 2;
    output.append(code_string.charAt(0));
}
</code></pre>

<p>BTW, it would look better if it was a for loop:</p>

<pre><code>for (int i = 0; concat.charAt(i++) == '0' &amp;&amp; concat.charAt(i++) == '0';) {
    output.append(code_string.charAt(0));
}
</code></pre>

<p>Also, probably you've made a mistake while copying the code: You haven't declared a variable named <code>concat</code>. It's <code>input</code>.</p>

<p>Last note: If you're going to use this encoding for things other than Bitcoin addresses (which have checksum), make sure that <code>i</code> will always be smaller than <code>concat.length()</code></p>

CC BY-SA 3.0
2018-04-06T07:33:06.567Z
2018-04-06T07:33:06.567Z
MCCCS
<p>Step 8 of <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="nofollow noreferrer">this</a> wiki page gives this hex value: <code>00010966776006953D5567439E5E39F86A0D273BEED61967F6</code></p>

<p>Step 9 converts it to this base58 string:<code>16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</code></p>

<p>I'm trying to implement the conversion function using the pseudocode from <a href="https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart" rel="nofollow noreferrer">this</a> wiki page. This is my implementation (in Java):</p>

<pre><code>    String input = "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
    BigInteger bigInteger = new BigInteger(input , 16);
    String code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    StringBuilder output = new StringBuilder();

    while(bigInteger.compareTo(BigInteger.ZERO) == 1){
        BigInteger[] divAndRemainder = bigInteger.divideAndRemainder(BigInteger.valueOf(58));
        output.append(code_string.charAt(divAndRemainder[1].intValue()));
        bigInteger = divAndRemainder[0];
    }

    int i=0;
    while(concat.charAt(i) == '0'){
        i++;
        output.append(code_string.charAt(0));
    }
    System.out.println(output.reverse());
</code></pre>

<p>This prints out <code>1116UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM</code>. This is close to what the wiki page produces, but not quite, there are 2 extra leading <code>1</code>s. This is from the 2 leading <code>0</code>s in the input string. Why doesn't the wiki example get my result?</p>

CC BY-SA 3.0
2018-04-04T03:55:52.103Z
the leading zeros are not covered correctly. Haven't looked into the code, but here is a very good discussion thread, including java references: https://bitcointalk.org/index.php?topic=1026.0 - also there is this java implementation here: http://rosettacode.org/wiki/Bitcoin/address_validation#Java
pebwindkraft
<address><address-generation><java><base58><base58check>
Why does my implementation of hex to Base58check produce different results than the example?
David says Reinstate Monica
# Why does my implementation of hex to Base58check produce different results than the example?

(Asked by: username_0 on 2018-04-04)

Step 8 of [this](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses) wiki page gives this hex value: `00010966776006953D5567439E5E39F86A0D273BEED61967F6`

Step 9 converts it to this base58 string:`16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM`

I'm trying to implement the conversion function using the pseudocode from [this](https://en.bitcoin.it/wiki/Base58Check_encoding#Base58_symbol_chart) wiki page. This is my implementation (in Java):

```
    String input = "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
    BigInteger bigInteger = new BigInteger(input , 16);
    String code_string = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
    StringBuilder output = new StringBuilder();

    while(bigInteger.compareTo(BigInteger.ZERO) == 1){
        BigInteger[] divAndRemainder = bigInteger.divideAndRemainder(BigInteger.valueOf(58));
        output.append(code_string.charAt(divAndRemainder[1].intValue()));
        bigInteger = divAndRemainder[0];
    }

    int i=0;
    while(concat.charAt(i) == '0'){
        i++;
        output.append(code_string.charAt(0));
    }
    System.out.println(output.reverse());

```

This prints out `1116UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM`. This is close to what the wiki page produces, but not quite, there are 2 extra leading `1`s. This is from the 2 leading `0`s in the input string. Why doesn't the wiki example get my result?

**Tags:** address, address-generation, java, base58, base58check

> **Comments:**
> **username_1**: the leading zeros are not covered correctly. Haven't looked into the code, but here is a very good discussion thread, including java references: https://bitcointalk.org/index.php?topic=1026.0 - also there is this java implementation here: http://rosettacode.org/wiki/Bitcoin/address_validation#Java  

---

## 1 answers

### Answer by username_2 on 2018-04-06. Score: 1

Each hexadecimal character has four bits of information. Two hex characters contains eight bits of information, so they form a byte.

For each byte in front of the address, a `1` should be put. Since two characters form a byte:

```
int i=0;
while(concat.charAt(i) == '0' && concat.charAt(i + 1) == '0'){
    i += 2;
    output.append(code_string.charAt(0));
}

```

BTW, it would look better if it was a for loop:

```
for (int i = 0; concat.charAt(i++) == '0' && concat.charAt(i++) == '0';) {
    output.append(code_string.charAt(0));
}

```

Also, probably you've made a mistake while copying the code: You haven't declared a variable named `concat`. It's `input`.

Last note: If you're going to use this encoding for things other than Bitcoin addresses (which have checksum), make sure that `i` will always be smaller than `concat.length()`


---

