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

user6091: 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.

user-1: 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:

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.

12049: <p>How to calculate transaction size from its hex-encoded representation, so i can include transaction fees per kb ?</p>
 6091: <p>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.</p>
 -1: <p>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#:</p>

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

   Byte[] byteArrayFromHexDump = StringToByteArray("0100000009c9ab....");
   Int32 byteArraySizeInBytes = byteArrayFromHexDump.Length;
</code></pre>

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

<p><code>(numberOfInputs * 148) + (numberOfOutputs * 34) + 10 (+/-) numberOfInputs</code></p>

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

<p><img src="https://i.stack.imgur.com/lDyge.jpg" alt="raw tx factory up to hex"></p>

<p>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. </p>

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

<p>You can use <a href="http://brainwallet.org/#tx" rel="nofollow noreferrer">brainwallet</a> to decode a hex dump and see its contents as a human-readable JSON representation:</p>

<pre><code>{
"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
    }
]
}
</code></pre>

<p>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:</p>

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