<p>Ok so i found the solution. The "hashtx, hashblock" etc messages are sent as raw binary bytes which then need to be converted to hexadecimal.</p>

<p>Here is my code. </p>

<pre><code>public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    // Synchronize with the publisher
    subscriber.connect("tcp://192.168.137.211:28332");

    //subscriber.subscribe("rawblock".getBytes());
    //subscriber.subscribe("rawtx".getBytes());
    //subscriber.subscribe("hashtx".getBytes());
    subscriber.subscribe("hashblock".getBytes());

    System.out.println("Subscribed.. Waiting for messages.");
    while (true) {

        ZMsg zMsg = ZMsg.recvMsg(subscriber);
        System.out.println("New message received!");
        int messageNumber = 0;
        for (ZFrame f: zMsg) {
            byte[] bytes = f.getData();
            System.out.println("Message number: " + messageNumber + " | Byte array length: " + bytes.length);
            if (messageNumber == 0){
                String messageType = new String(bytes);
                System.out.println("Message type: " + messageType);
            } else if (messageNumber == 1) {
                String message = Converter.bin2hex(bytes);
                System.out.println("Message: " + message);
            }
            messageNumber++;
        }

    }
}
</code></pre>

<p>and the binary to hex converter class</p>

<pre><code>import java.util.Formatter;
public class Converter
{
private Converter() {
    throw new IllegalStateException("Utility class");
}

public static String bin2hex(byte[] bytes)
{
    Formatter f = new Formatter();
    try {
        for (byte c : bytes)
            f.format("%02X",c);

        return (f.toString().toLowerCase());
    } finally {
        f.close();
    }
}

public static String hex2bin(String hexString)
{
    if (!hexString.matches("^[0-9a-fA-F]+$")) {
        return null;
    }
    StringBuilder output = new StringBuilder();
    for (int i = 0; i &lt; hexString.length(); i+=2) {
        String str = hexString.substring(i, i+2);
        output.append((char)Integer.parseInt(str, 16));
    }
    return output.toString();
}
}
</code></pre>

CC BY-SA 3.0
2018-02-03T08:37:39.653Z
2018-02-03T08:37:39.653Z
dudious maximus
<p>Bitcoin Core does not give you strings for the data from ZMQ. It provides the raw bytes of the transactions, blocks, and their hashes. You will need to interpret the received data as byte arrays and handle them appropriately.</p>

CC BY-SA 3.0
2018-02-03T05:47:35.240Z
2018-02-03T05:47:35.240Z
Ava Chow
<p>This is the code that i have. I receive messages from the zeroMQ however a hashtx message for example looks like this --> "hashtx Ř�����v����|(8d �!��v��&amp;�-�k�". Any help in turning that into a real hashtx is appreciated :)    </p>

<pre><code>public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://192.168.137.211:28332");

    subscriber.subscribe("rawblock".getBytes());
    subscriber.subscribe("hashblock".getBytes());
    subscriber.subscribe("rawtx".getBytes());
    subscriber.subscribe("hashtx".getBytes());

    while (!msg.equalsIgnoreCase("END")) {

        String printMe = new String(subscriber.recv());
        System.out.println(printMe);

    }

}
</code></pre>

CC BY-SA 3.0
2018-02-02T13:34:16.913Z
I wish I had an answer of the to of my head, but I can tell you this:  zeromq isn't a format, it's a transport medium.  It can transport any format, but you are aiming it's a string.  It's not.  You need to read the spec to find out how to interpret the bytes you receive.
Jestin
<bitcoind><java><zeromq>
How do i parse the zeroMQ messages in java?
dudious maximus
# How do i parse the zeroMQ messages in java?

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

This is the code that i have. I receive messages from the zeroMQ however a hashtx message for example looks like this --> "hashtx Ř�����v����|(8d �!��v��&�-�k�". Any help in turning that into a real hashtx is appreciated :)    
public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    subscriber.connect("tcp://192.168.137.211:28332");

    subscriber.subscribe("rawblock".getBytes());
    subscriber.subscribe("hashblock".getBytes());
    subscriber.subscribe("rawtx".getBytes());
    subscriber.subscribe("hashtx".getBytes());

    while (!msg.equalsIgnoreCase("END")) {

        String printMe = new String(subscriber.recv());
        System.out.println(printMe);

    }

}

**Tags:** bitcoind, java, zeromq

> **Comments:**
> **username_1**: I wish I had an answer of the to of my head, but I can tell you this:  zeromq isn't a format, it's a transport medium.  It can transport any format, but you are aiming it's a string.  It's not.  You need to read the spec to find out how to interpret the bytes you receive.  

---

## 2 answers

### Answer by username_0 on 2018-02-03. Score: 2 | Accepted answer

Ok so i found the solution. The "hashtx, hashblock" etc messages are sent as raw binary bytes which then need to be converted to hexadecimal.
Here is my code. 
public static void main(String[] args) {
    ZMQ.Context context = ZMQ.context(1);

    // Connect our subscriber socket
    ZMQ.Socket subscriber = context.socket(ZMQ.SUB);
    // Synchronize with the publisher
    subscriber.connect("tcp://192.168.137.211:28332");

    //subscriber.subscribe("rawblock".getBytes());
    //subscriber.subscribe("rawtx".getBytes());
    //subscriber.subscribe("hashtx".getBytes());
    subscriber.subscribe("hashblock".getBytes());

    System.out.println("Subscribed.. Waiting for messages.");
    while (true) {

        ZMsg zMsg = ZMsg.recvMsg(subscriber);
        System.out.println("New message received!");
        int messageNumber = 0;
        for (ZFrame f: zMsg) {
            byte[] bytes = f.getData();
            System.out.println("Message number: " + messageNumber + " | Byte array length: " + bytes.length);
            if (messageNumber == 0){
                String messageType = new String(bytes);
                System.out.println("Message type: " + messageType);
            } else if (messageNumber == 1) {
                String message = Converter.bin2hex(bytes);
                System.out.println("Message: " + message);
            }
            messageNumber++;
        }

    }
}

and the binary to hex converter class
import java.util.Formatter;
public class Converter
{
private Converter() {
    throw new IllegalStateException("Utility class");
}

public static String bin2hex(byte[] bytes)
{
    Formatter f = new Formatter();
    try {
        for (byte c : bytes)
            f.format("%02X",c);

        return (f.toString().toLowerCase());
    } finally {
        f.close();
    }
}

public static String hex2bin(String hexString)
{
    if (!hexString.matches("^[0-9a-fA-F]+$")) {
        return null;
    }
    StringBuilder output = new StringBuilder();
    for (int i = 0; i < hexString.length(); i+=2) {
        String str = hexString.substring(i, i+2);
        output.append((char)Integer.parseInt(str, 16));
    }
    return output.toString();
}
}


---

### Answer by username_2 on 2018-02-03. Score: 2

Bitcoin Core does not give you strings for the data from ZMQ. It provides the raw bytes of the transactions, blocks, and their hashes. You will need to interpret the received data as byte arrays and handle them appropriately.


---

