<p>A shorter version could be:</p>

<pre><code>String hex = DatatypeConverter.printHexBinary(tx.unsafeBitcoinSerialize());
</code></pre>

<p>Where the hex converter used is standard at least since Java 7:</p>

<p><a href="http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html" rel="nofollow">http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html</a></p>

CC BY-SA 3.0
2014-09-12T15:49:34.080Z
If you use `unsafeBitcoinSerialize`, make a note of [the warnings in the API](http://plan99.net/~mike/bitcoinj/0.11/com/google/bitcoin/core/Message.html#unsafeBitcoinSerialize()).
Nick ODell
CC BY-SA 3.0
2014-09-12T19:33:26.840Z
As for the example, the result is only temporary used to convert it to hex and the discarded, so it's safe to be used this way.
Problems arise only if you decide to modify the returned buffer.
Alessandro Polverini
CC BY-SA 3.0
2014-09-12T12:51:39.340Z
2014-09-12T12:51:39.340Z
Alessandro Polverini
<p>I want to use this tool: <a href="http://blockchain.info/pushtx" rel="nofollow">http://blockchain.info/pushtx</a> to push transaction. I don't understand the input data. It says it is hex string representation. I've read information about that in the raw transaction of the RPC API, but I am working with bitcoinj, it there a way to get this hex string ?</p>

<p>Thank you</p>

<p>EDIT: I want to do that to compose and broadcast a transaction</p>

<p>For now I did this:</p>

<pre><code>final StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
try {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    transaction.bitcoinSerialize(os);
    byte[] bytes = os.toByteArray();
    for (byte b : bytes) {
        formatter.format("%02x", b);  
    }

    return sb.toString();
}catch (IOException e) {
    return "Couldn't serialize to hex string.";
} finally {
    formatter.close();
}
</code></pre>

CC BY-SA 3.0
2013-03-18T17:35:53.643Z
To compose and broadcast a transaction, or to re-broadcast a transaction that the network has already seen?
Stephen Gornick
CC BY-SA 3.0
2013-03-18T18:43:39.357Z
To compose and broadcast a transaction.
Baptiste Pernet
CC BY-SA 3.0
2014-08-28T09:34:31.053Z
I am using your code and it works very well, thanks
Riccardo Casatta
<transactions><bitcoinj>
How to get hex string from transaction in bitcoinj
Baptiste Pernet
# How to get hex string from transaction in bitcoinj

(Asked by: username_0 on 2013-03-18)

I want to use this tool: http://blockchain.info/pushtx to push transaction. I don't understand the input data. It says it is hex string representation. I've read information about that in the raw transaction of the RPC API, but I am working with bitcoinj, it there a way to get this hex string ?
Thank you
EDIT: I want to do that to compose and broadcast a transaction
For now I did this:
final StringBuilder sb = new StringBuilder();
Formatter formatter = new Formatter(sb);
try {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    transaction.bitcoinSerialize(os);
    byte[] bytes = os.toByteArray();
    for (byte b : bytes) {
        formatter.format("%02x", b);  
    }

    return sb.toString();
}catch (IOException e) {
    return "Couldn't serialize to hex string.";
} finally {
    formatter.close();
}

**Tags:** transactions, bitcoinj

> **Comments:**
> **username_1**: To compose and broadcast a transaction, or to re-broadcast a transaction that the network has already seen?  
> **username_0**: To compose and broadcast a transaction.  
> **username_2**: I am using your code and it works very well, thanks  

---

## 1 answers

### Answer by username_3 on 2014-09-12. Score: 3

A shorter version could be:
String hex = DatatypeConverter.printHexBinary(tx.unsafeBitcoinSerialize());

Where the hex converter used is standard at least since Java 7:
http://docs.oracle.com/javase/7/docs/api/javax/xml/bind/DatatypeConverter.html

> **Comments:**
> **username_4**: If you use `unsafeBitcoinSerialize`, make a note of [the warnings in the API](http://plan99.net/~mike/bitcoinj/0.11/com/google/bitcoin/core/Message.html#unsafeBitcoinSerialize()).  
> **username_3**: As for the example, the result is only temporary used to convert it to hex and the discarded, so it's safe to be used this way. Problems arise only if you decide to modify the returned buffer.  

---

