Take the 2-minute tour ×
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It's 100% free, no registration required.

How to calculate transaction size from its hex-encoded representation, so i can include transaction fees per kb ?

share|improve this question
    
I am creating simple transactions, 1 input and 1 output, based on George formula is lesser than 200 bytes. Could i set a fee lesser than mintxfee=0.1 mBTC without increasing confirmation time? –  Riccardo Casatta Apr 24 at 10:15
add comment

2 Answers 2

In order to get the size of the hexadecimal dump in bytes you need to convert it into an array of bytes and then get the array's length. Sample code in C#:

    public static Byte[] StringToByteArray(String hex)
    {
        return Enumerable.Range(0, hex.Length)
                         .Where(x => x % 2 == 0)
                         .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                         .ToArray();
    }

   Byte[] byteArrayFromHexDump = StringToByteArray("0100000009c9ab....");
   Int32 byteArraySizeInBytes = byteArrayFromHexDump.Length;

The formula for calculating the "send-to-address" transaction size in bitcoin is:

(numberOfInputs * 148) + (numberOfOutputs * 34) + 10 (+/-) numberOfInputs

On the other hand, the hex contains more information that just the number of the transaction's inputs and outputs:

raw tx factory up to hex

So the answer to your question is no, the transaction size cannot be calculated by its hex (that also needs to be signed before it can be dispatched so we need some extra space for the signatures as well), at least not unless the hex is decoded.

If decoded, then you can count the number of the in and out arrays' elements and calculate the transaction size with the formula provided above.

You can use brainwallet to decode a hex dump and see its contents as a human-readable JSON representation:

{
"hash": "9bf1674adaba6ffff98929ebc844f81da4b389c8b0e2fa241bec
"ver": 1,
"vin_sz": 9,
"vout_sz": 8,
"lock_time": 0,
"size": 651,
"in": [
    {
        "prev_out": {
            "hash": "664720e924865cbd8f59841f671b61a642c28e93
            "n": 0
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "859c582f1dae337a778def754514955b83b1bdb4
            "n": 1
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "1afb669e147fdd376c34e92661d1ea1e2b6e6e95
            "n": 1
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "785991ed3f1cbe2e6e34d6beee5dbd0aca64c8b0
            "n": 1
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "f117fdf9ef322f9440a2d6b6fde026d99e3857b0
            "n": 0
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "1729de5be595a7dcdd9b1b9794f417c07298f7af
            "n": 0
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "835f81ee3f06971ced08f70b4a674b982448d9f8
            "n": 0
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "05f750e72ae0db4cc4680870f59e180aa6af3b6d
            "n": 1
        },
        "scriptSig": "",
        "sequence": 4294967295
    },
    {
        "prev_out": {
            "hash": "25f1112ad8e673ca3594d0d2cff98e8480a67f87
            "n": 0
        },
        "scriptSig": "",
        "sequence": 4294967295
    }
],
"out": [
    {
        "value": "1.59899677",
        "scriptPubKey": "OP_DUP OP_HASH160 5e27c91d971e4c8730
    },
    {
        "value": "3.26536812",
        "scriptPubKey": "OP_DUP OP_HASH160 5a71ac3a827d51b142
    },
    {
        "value": "2.55999483",
        "scriptPubKey": "OP_DUP OP_HASH160 23b7530a00dd7951e1
    },
    {
        "value": "64.34987007",
        "scriptPubKey": "OP_DUP OP_HASH160 91f13fed21eb72c7b9
    },
    {
        "value": "2.37899520",
        "scriptPubKey": "OP_DUP OP_HASH160 b46386342596a1be16
    },
    {
        "value": "58.07088275",
        "scriptPubKey": "OP_DUP OP_HASH160 dbd7e62f63bfbdd996
    },
    {
        "value": "38.45263760",
        "scriptPubKey": "OP_DUP OP_HASH160 6714f3fb946e4f9f6c
    },
    {
        "value": "0.95035476",
        "scriptPubKey": "OP_DUP OP_HASH160 6b3f14bc5722e6dcdc
    }
]
}

In this transaction (with a 651 bytes long hex) there are 9 inputs and 8 outputs. Applying these figures to the transaction size calculation formula gives us:

(9 * 148) + (8 * 34) + 10 (+/-) 9 = 1614 +- 9 = 1605 to 1623 bytes, which is totally different from the 651 bytes of the hex dump.

share|improve this answer
1  
Why are you going into all this detail about number of inputs and outputs and how to decode transaction data? Are you thinking the OP has something other than a finished, signed transaction that is ready to be included in the block chain? If so, what structure are you talking about and where is it documented? –  David Grayson Apr 24 at 15:38
1  
The OP doesn't specify if the hex is signed or not so claiming that he's referring to a "finished, signed transaction that is ready to be included in the block chain" is an arbitrary inference that may be true but is not necessarily true. A hex is still a hex even before signing and is available to the user as soon as the raw transaction is constructed via createrawtransaction. –  George Kimionis Apr 24 at 17:37
add comment

If your hex string looks something like "12abcd" then it uses two hex characters per byte. Just take the length of the hex string, divide it by two to get the number of bytes in the transaction, and then divide by 1024 to get the number of KiB.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.