How to decode raw tx hex programmatically 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++
This is specified thoroughly on the Bitcoin Wiki.

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 of libraries 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.