<p>You cannot convert json string to hex representation. Transactions have their own binary format (you can read in wiki: <a href="https://en.bitcoin.it/wiki/Transactions" rel="nofollow">https://en.bitcoin.it/wiki/Transactions</a>). To create transaction in hex format, you can use bitcoin-qt (or bitcoind) function <code>createrawtransaction</code>. Example of using:</p>

<pre><code>createrawtransaction
'[
    { "txid": "10d5b7f61be2fa2e5979043a7071d3dbd371bc72cefea22e43b2dd54a6e27a99", "vout": 1 }
]'
'{
    "14rbFswzZfkPGkbFZ7Ffj2qhQA1omvgiUx": 0.099
}'
</code></pre>

<p>This call will return raw transaction in hex:</p>

<pre><code>0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000
</code></pre>

<p>After that, you must call <code>signrawtransaction</code>:</p>

<pre><code>signrawtransaction 0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000
</code></pre>

<p>This call will return raw transaction in hex, which you can send to network using <code>sendrawtransaction</code>.</p>

<p>Note: be very careful, you can make mistake in amount, and lose your bitcoins.</p>

CC BY-SA 4.0
2023-11-28T20:02:28.707Z
what is the data format expected in outputs passed by that `createrawtransaction` function?
David 天宇 Wong
CC BY-SA 4.0
2023-11-29T20:51:17.153Z
@David天宇Wong https://developer.bitcoin.org/reference/rpc/createrawtransaction.html
Zergatul
CC BY-SA 3.0
2014-02-04T10:07:09.247Z
2014-02-04T10:23:31.953Z
2014-02-04T10:23:31.953Z
Zergatul
<p>I've been trying to broadcast a transaction using sendrawtransaction of bitcoind.
I keep getting an error : "TX decode failed.", Code -22.</p>

<p>I suspect I'm not properly formatting the transaction into hex before attempting to send.</p>

<pre><code> btc.sendrawtransaction(new_tx.to_json.unpack("H*")[0])
</code></pre>

<p>Here is a representation of my transaction in hex :
         7b0a20202268617368223a22333235343463303630316532323932626566303039353265396636303437    38373665383637656336373664643036306163323365356563666361386135656335222c0a202022766572223a312c0a20202276696e5f737a223a312c0a202022766f75745f737a223a312c0a2020226c6f636b5f74696d65223a302c0a20202273697a65223a3139332c0a202022696e223a5b0a202020207b0a20202020202022707265765f6f7574223a7b0a20202020202020202268617368223a2239636239643035363965373135346662383565343538343162633564336663633734393961636431633831326264666565366535653961386433323933313664222c0a2020202020202020226e223a300a2020202020207d2c0a20202020202022736372697074536967223a22333034363032323130303935393761353761373036326537303762323830323439663662653831663434356263636663313038336264643231326465643231353961316534653137616330323231303065346163383535366362653237316163363631653965383364306439663564616333633032393330303263613836336230386431386236386434323361636461303120303333396535613566383031343165643664663566343436326461623831323161353461333365386163633161656235326639626264616465613138663033393963220a202020207d0a20205d2c0a2020226f7574223a5b0a202020207b0a2020202020202276616c7565223a22302
3031303030303030222c0a202020202020227363726970745075624b6579223a224f505f445550204f505f48415348313630203662343633623439373835393930386439646236\
3562393734623434333532386232396132613932204f505f455155414c564552494659204f505f434845434b534947220a202020207d0a20205d0a7ee</p>

<ul>
<li>Is this how I transform a transaction into hex in ruby ?</li>
<li>Is there anything else I'm missing ? </li>
</ul>

CC BY-SA 3.0
2014-02-04T09:30:04.547Z
Could you give more info? For example, what is returned by `new_tx.to_json.unpack("H*")[0]`?
Zergatul
CC BY-SA 3.0
2014-02-04T09:32:35.483Z
new_tx.to_json.unpack("H*")[0] is supposed to return the hex representation of the string.
Attilah
CC BY-SA 3.0
2014-02-04T09:35:11.900Z
How we can analyze your problem, if we don't see your transaction in hex?
Zergatul
CC BY-SA 3.0
2014-02-04T09:40:40.380Z
Looks like your transaction is totally invalid. Every transaction must start with 4 bytes version integer: 01000000 (in hex)
Zergatul
CC BY-SA 3.0
2014-02-04T09:47:08.207Z
this transaction is to be sent on testnet. should those also start with 01000000 ?
Attilah
CC BY-SA 3.0
2014-02-04T09:50:53.650Z
yes, there are no differences in version data
Zergatul
CC BY-SA 3.0
2014-02-04T09:53:52.447Z
hmmm, what is the best way to transform a transaction in json format into hex ?
Attilah
<bitcoind><api>
How to convert a transaction into hex before using the sendrawtransaction API?
Attilah
# How to convert a transaction into hex before using the sendrawtransaction API?

(Asked by: username_0 on 2014-02-04)

I've been trying to broadcast a transaction using sendrawtransaction of bitcoind.
I keep getting an error : "TX decode failed.", Code -22.
I suspect I'm not properly formatting the transaction into hex before attempting to send.
 btc.sendrawtransaction(new_tx.to_json.unpack("H*")[0])

Here is a representation of my transaction in hex :
         7b0a20202268617368223a22333235343463303630316532323932626566303039353265396636303437    38373665383637656336373664643036306163323365356563666361386135656335222c0a202022766572223a312c0a20202276696e5f737a223a312c0a202022766f75745f737a223a312c0a2020226c6f636b5f74696d65223a302c0a20202273697a65223a3139332c0a202022696e223a5b0a202020207b0a20202020202022707265765f6f7574223a7b0a20202020202020202268617368223a2239636239643035363965373135346662383565343538343162633564336663633734393961636431633831326264666565366535653961386433323933313664222c0a2020202020202020226e223a300a2020202020207d2c0a20202020202022736372697074536967223a22333034363032323130303935393761353761373036326537303762323830323439663662653831663434356263636663313038336264643231326465643231353961316534653137616330323231303065346163383535366362653237316163363631653965383364306439663564616333633032393330303263613836336230386431386236386434323361636461303120303333396535613566383031343165643664663566343436326461623831323161353461333365386163633161656235326639626264616465613138663033393963220a202020207d0a20205d2c0a2020226f7574223a5b0a202020207b0a2020202020202276616c7565223a22302
3031303030303030222c0a202020202020227363726970745075624b6579223a224f505f445550204f505f48415348313630203662343633623439373835393930386439646236\
3562393734623434333532386232396132613932204f505f455155414c564552494659204f505f434845434b534947220a202020207d0a20205d0a7ee

Is this how I transform a transaction into hex in ruby ?
Is there anything else I'm missing ?

**Tags:** bitcoind, api

> **Comments:**
> **username_1**: Could you give more info? For example, what is returned by `new_tx.to_json.unpack("H*")[0]`?  
> **username_0**: new_tx.to_json.unpack("H*")[0] is supposed to return the hex representation of the string.  
> **username_1**: How we can analyze your problem, if we don't see your transaction in hex?  
> **username_1**: Looks like your transaction is totally invalid. Every transaction must start with 4 bytes version integer: 01000000 (in hex)  
> **username_0**: this transaction is to be sent on testnet. should those also start with 01000000 ?  
> **username_1**: yes, there are no differences in version data  
> **username_0**: hmmm, what is the best way to transform a transaction in json format into hex ?  

---

## 1 answers

### Answer by username_1 on 2014-02-04. Score: 4 | Accepted answer

You cannot convert json string to hex representation. Transactions have their own binary format (you can read in wiki: https://en.bitcoin.it/wiki/Transactions). To create transaction in hex format, you can use bitcoin-qt (or bitcoind) function createrawtransaction. Example of using:
createrawtransaction
'[
    { "txid": "10d5b7f61be2fa2e5979043a7071d3dbd371bc72cefea22e43b2dd54a6e27a99", "vout": 1 }
]'
'{
    "14rbFswzZfkPGkbFZ7Ffj2qhQA1omvgiUx": 0.099
}'

This call will return raw transaction in hex:
0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000

After that, you must call signrawtransaction:
signrawtransaction 0100000001997ae2a654ddb2432ea2fece72bc71d3dbd371703a0479592efae21bf6b7d5100100000000ffffffff01e00f9700000000001976a9142a495afa8b8147ec2f01713b18693cb0a85743b288ac00000000

This call will return raw transaction in hex, which you can send to network using sendrawtransaction.
Note: be very careful, you can make mistake in amount, and lose your bitcoins.

> **Comments:**
> **username_2**: what is the data format expected in outputs passed by that `createrawtransaction` function?  
> **username_1**: @David天宇Wong https://developer.bitcoin.org/reference/rpc/createrawtransaction.html  

---

