<p>This is specified thoroughly on the <a href="https://en.bitcoin.it/wiki/Transaction" rel="noreferrer">Bitcoin Wiki</a>.</p>

<p>The first 4 bytes are the version number, the next 1-9 bytes are the number of inputs (almost never more than 1 byte) and so on.</p>

<p>Programitically, there are a <a href="https://github.com/petertodd/python-bitcoinlib" rel="noreferrer">ton</a> of <a href="https://bitcoinj.github.io/" rel="noreferrer">libraries</a> that take the raw text and spit out the json as the bitcoin RPC call does in your example. They all use the same specification, though, so you can create the code yourself or use something already written.</p>

CC BY-SA 4.0
2018-09-20T13:14:25.973Z
it would be cool if you point to the method on these libs which does the conversion
Sunny
CC BY-SA 4.0
2018-12-31T12:50:25.403Z
Hi,
I'm using bitcoinj and I want create raw transaction without creating wallet instance in android.
Can you please help me how can I do that?
Kush
CC BY-SA 3.0
2016-01-24T03:23:10.407Z
2016-01-24T03:23:10.407Z
Jimmy Song
<p><code>createrawtransaction(...)</code> in testnet returns this hex:</p>

<pre><code>01000000010470c3139dc0f0882f98d75ae5bf957e68dadd32c5f81261c0b13e85f592ff7b0000000000ffffffff02b286a61e000000001976a9140f39a0043cf7bdbe429c17e8b514599e9ec53dea88ac01000000000000001976a9148a8c9fd79173f90cf76410615d2a52d12d27d21288ac00000000
</code></pre>

<p>I know how to decode this with <code>decoderawtransaction</code> but if I want to do this programmatically how can I do it? In other words, what is the exact encoding/format of this hex and how can I decode it into plain text or json using c# or some other language?</p>

<p>The decoded text should be:</p>

<pre><code>{
    "addresses": [
        "miXoR8DUo5tQJyr6rkKMF2WKkrBRTb8tEV", 
        "mguTTnqj3ASi3YYtzGAoYrUqXGRjCTuovG", 
        "mt9Y1HCL8ntHrT5jX9kx1G86rGZ2bcyajh"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confidence": 0, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 500000, 
    "hash": "b020bdec4e92cb69db93557dcbbfcc73076fc01f6828e41eb3ef5f628414ee62", 
    "inputs": [
        {
            "addresses": [
                "miXoR8DUo5tQJyr6rkKMF2WKkrBRTb8tEV"
            ], 
            "age": 2596, 
            "output_index": 0, 
            "output_value": 514729939, 
            "prev_hash": "7bff92f5853eb1c06112f8c532ddda687e95bfe55ad7982f88f0c09d13c37004", 
            "script": "", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "lock_time": 0, 
    "outputs": [
        {
            "addresses": [
                "mguTTnqj3ASi3YYtzGAoYrUqXGRjCTuovG"
            ], 
            "script": "76a9140f39a0043cf7bdbe429c17e8b514599e9ec53dea88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 514229938
        }, 
        {
            "addresses": [
                "mt9Y1HCL8ntHrT5jX9kx1G86rGZ2bcyajh"
            ], 
            "script": "76a9148a8c9fd79173f90cf76410615d2a52d12d27d21288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 1
        }
    ], 
    "preference": "high", 
    "received": "2016-01-22T12:58:23.224606219Z", 
    "relayed_by": "54.167.150.115, 127.0.0.1", 
    "size": 119, 
    "total": 514229939, 
    "ver": 1, 
    "vin_sz": 1, 
    "vout_sz": 2
}
</code></pre>

<p>Reference: <a href="https://github.com/bitcoin/bitcoin/blob/17ef279304a94ec01a8525189b80c163d931b1cf/src/rpcrawtransaction.cpp#L420" rel="noreferrer">decoderawtransaction in C++</a></p>

<raw-transaction><encoding>
How to decode raw tx hex programmatically
Doug Peters
# How to decode raw tx hex programmatically

(Asked by: username_0 on 2016-01-22)

`createrawtransaction(...)` in testnet returns this hex:

```
01000000010470c3139dc0f0882f98d75ae5bf957e68dadd32c5f81261c0b13e85f592ff7b0000000000ffffffff02b286a61e000000001976a9140f39a0043cf7bdbe429c17e8b514599e9ec53dea88ac01000000000000001976a9148a8c9fd79173f90cf76410615d2a52d12d27d21288ac00000000

```

I know how to decode this with `decoderawtransaction` but if I want to do this programmatically how can I do it? In other words, what is the exact encoding/format of this hex and how can I decode it into plain text or json using c# or some other language?

The decoded text should be:

```
{
    "addresses": [
        "miXoR8DUo5tQJyr6rkKMF2WKkrBRTb8tEV", 
        "mguTTnqj3ASi3YYtzGAoYrUqXGRjCTuovG", 
        "mt9Y1HCL8ntHrT5jX9kx1G86rGZ2bcyajh"
    ], 
    "block_height": -1, 
    "block_index": -1, 
    "confidence": 0, 
    "confirmations": 0, 
    "double_spend": false, 
    "fees": 500000, 
    "hash": "b020bdec4e92cb69db93557dcbbfcc73076fc01f6828e41eb3ef5f628414ee62", 
    "inputs": [
        {
            "addresses": [
                "miXoR8DUo5tQJyr6rkKMF2WKkrBRTb8tEV"
            ], 
            "age": 2596, 
            "output_index": 0, 
            "output_value": 514729939, 
            "prev_hash": "7bff92f5853eb1c06112f8c532ddda687e95bfe55ad7982f88f0c09d13c37004", 
            "script": "", 
            "script_type": "pay-to-pubkey-hash", 
            "sequence": 4294967295
        }
    ], 
    "lock_time": 0, 
    "outputs": [
        {
            "addresses": [
                "mguTTnqj3ASi3YYtzGAoYrUqXGRjCTuovG"
            ], 
            "script": "76a9140f39a0043cf7bdbe429c17e8b514599e9ec53dea88ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 514229938
        }, 
        {
            "addresses": [
                "mt9Y1HCL8ntHrT5jX9kx1G86rGZ2bcyajh"
            ], 
            "script": "76a9148a8c9fd79173f90cf76410615d2a52d12d27d21288ac", 
            "script_type": "pay-to-pubkey-hash", 
            "value": 1
        }
    ], 
    "preference": "high", 
    "received": "2016-01-22T12:58:23.224606219Z", 
    "relayed_by": "54.167.150.115, 127.0.0.1", 
    "size": 119, 
    "total": 514229939, 
    "ver": 1, 
    "vin_sz": 1, 
    "vout_sz": 2
}

```

Reference: [decoderawtransaction in C++](https://github.com/bitcoin/bitcoin/blob/17ef279304a94ec01a8525189b80c163d931b1cf/src/rpcrawtransaction.cpp#L420)

**Tags:** raw-transaction, encoding


---

## 1 answers

### Answer by username_1 on 2016-01-24. Score: 6

This is specified thoroughly on the [Bitcoin Wiki](https://en.bitcoin.it/wiki/Transaction).

The first 4 bytes are the version number, the next 1-9 bytes are the number of inputs (almost never more than 1 byte) and so on.

Programitically, there are a [ton](https://github.com/petertodd/python-bitcoinlib) of [libraries](https://bitcoinj.github.io/) that take the raw text and spit out the json as the bitcoin RPC call does in your example. They all use the same specification, though, so you can create the code yourself or use something already written.

> **Comments:**
> **username_2**: it would be cool if you point to the method on these libs which does the conversion  
> **username_3**: Hi, I'm using bitcoinj and I want create raw transaction without creating wallet instance in android. Can you please help me how can I do that?  

---

