<p>
You should <a href="https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java">convert hex representation of bytes to byte array</a> (not String to byte array). Then make hash. Then convert the byte array back to hex representation of bytes in String.</p>

<p>The whole solution will look like:</p>

<pre><code>import java.security.*;

class HashExample {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String string = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";
        string = "80" + string;
        byte[] data = hexStringToByteArray(string);
        byte[] digest = MessageDigest.getInstance("SHA-256").digest(hexStringToByteArray(string));
        String result = bytesToHex(digest);
        System.out.println(result);
        //prints: 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i &lt; len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) &lt;&lt; 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b &amp; 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}
</code></pre>

<p>Of course this is just example. In production code I recommend to use one of <a href="https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java">the available libraries which can perform this task</a>.</p>

CC BY-SA 4.0
2021-11-11T09:59:45.060Z
hexStringToByteArray can be done much simpler:
`byte[] bytes = new BigInteger(privateKeyHex, 16).toByteArray();`
eztam
CC BY-SA 3.0
2017-12-02T08:33:14.580Z
2017-12-02T08:46:42.603Z
2017-12-02T08:46:42.603Z
Michał Zabielski
<p>I have a question concerning Java and sha-256.</p>

<p>I try to do these steps discribed here to create a WIF key:
<a href="https://en.bitcoin.it/wiki/Wallet_import_format" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Wallet_import_format</a></p>

<ol>
<li>step: </li>
</ol>

<blockquote>
  <p>String string
  = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";</p>
</blockquote>

<ol start="2">
<li>step (ad 80 in front):</li>
</ol>

<blockquote>
  <p>string = "80" + string;</p>
</blockquote>

<ol start="3">
<li>perform sha-256:

<ul>
<li>here is my mistake. I can perform sha-256 on step 2 but the result is wrong. The reason seems to be perform the hash on a String and not on binary. 
I get:</li>
</ul></li>
</ol>

<blockquote>
  <p>e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09</p>
</blockquote>

<p>But I should get:</p>

<blockquote>
  <p>8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592</p>
</blockquote>

<p>I dont get how to convert it to binary. Can someone help me?</p>

<p>Its the same problem as here (<a href="https://bitcoin.stackexchange.com/questions/44283/correct-way-to-convert-a-256-bit-private-key-to-wif">Correct way to convert a 256-bit private key to WIF?</a>) but I dont understand the answer. I also cant reply to the thread because I dont have enough reputation.</p>

<p>Thank you :)</p>

<private-key><java><wallet-import-format>
Java: way to convert a 256-bit private key to WIF?
A.c
# Java: way to convert a 256-bit private key to WIF?

(Asked by: username_0 on 2017-12-01)

I have a question concerning Java and sha-256.

I try to do these steps discribed here to create a WIF key:
<https://en.bitcoin.it/wiki/Wallet_import_format>

1. step:

> String string
> = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";

2. step (ad 80 in front):

> string = "80" + string;

3. perform sha-256:
   * here is my mistake. I can perform sha-256 on step 2 but the result is wrong. The reason seems to be perform the hash on a String and not on binary.
     I get:

> e2e4146a36e9c455cf95a4f259f162c353cd419cc3fd0e69ae36d7d1b6cd2c09

But I should get:

> 8147786C4D15106333BF278D71DADAF1079EF2D2440A4DDE37D747DED5403592

I dont get how to convert it to binary. Can someone help me?

Its the same problem as here ([Correct way to convert a 256-bit private key to WIF?](https://bitcoin.stackexchange.com/questions/44283/correct-way-to-convert-a-256-bit-private-key-to-wif)) but I dont understand the answer. I also cant reply to the thread because I dont have enough reputation.

Thank you :)

**Tags:** private-key, java, wallet-import-format


---

## 1 answers

### Answer by username_1 on 2017-12-02. Score: 2 | Accepted answer

You should [convert hex representation of bytes to byte array](https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java) (not String to byte array). Then make hash. Then convert the byte array back to hex representation of bytes in String.

The whole solution will look like:

```
import java.security.*;

class HashExample {
    public static void main(String[] args) throws NoSuchAlgorithmException {
        String string = "0C28FCA386C7A227600B2FE50B7CAE11EC86D3BF1FBE471BE89827E19D72AA1D";
        string = "80" + string;
        byte[] data = hexStringToByteArray(string);
        byte[] digest = MessageDigest.getInstance("SHA-256").digest(hexStringToByteArray(string));
        String result = bytesToHex(digest);
        System.out.println(result);
        //prints: 8147786c4d15106333bf278d71dadaf1079ef2d2440a4dde37d747ded5403592
    }

    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
                    + Character.digit(s.charAt(i+1), 16));
        }
        return data;
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuffer result = new StringBuffer();
        for (byte b : bytes) result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
        return result.toString();
    }
}

```

Of course this is just example. In production code I recommend to use one of [the available libraries which can perform this task](https://stackoverflow.com/questions/140131/convert-a-string-representation-of-a-hex-dump-to-a-byte-array-using-java).

> **Comments:**
> **username_2**: hexStringToByteArray can be done much simpler: `byte[] bytes = new BigInteger(privateKeyHex, 16).toByteArray();`  

---

