<p>bitcoin protocol use little endian order in it's byte array.</p>

CC BY-SA 4.0
2023-10-22T18:43:52.540Z
2023-10-22T18:43:52.540Z
Monte Conte
<p>Because it's encoded as a <a href="https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/" rel="nofollow noreferrer">little endian</a> number, meaning the least significant byte goes first.</p>
<p>Example:</p>
<p>Number 1234 would be encoded as byte sequence <code>000004D2</code> when encoded as 4-byte big endian, and it would be encoded as byte sequence <code>D2040000</code> when encoded as 4-byte little endian.</p>

CC BY-SA 4.0
2023-10-22T18:47:58.047Z
2023-10-23T13:41:38.960Z
2023-10-23T13:41:38.960Z
bca-0353f40e
<p>According to <a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">the protocol documentation for tx</a>, the first field on the transaction data is a version number of size 4.</p>
<p>Based on this, I am trying to format the array of bytes needed to calculate the TX hash for the genesis TX, so I retrieve the block information from my node:</p>
<pre><code>{
  &quot;hash&quot;: &quot;000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f&quot;,
  &quot;confirmations&quot;: 708859,
  &quot;height&quot;: 0,
  &quot;version&quot;: 1,
  &quot;versionHex&quot;: &quot;00000001&quot;,
  &quot;merkleroot&quot;: &quot;4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b&quot;,
  &quot;time&quot;: 1231006505,
  &quot;mediantime&quot;: 1231006505,
  &quot;nonce&quot;: 2083236893,
  &quot;bits&quot;: &quot;1d00ffff&quot;,
  &quot;difficulty&quot;: 1,
  &quot;chainwork&quot;: &quot;0000000000000000000000000000000000000000000000000000000100010001&quot;,
  &quot;nTx&quot;: 1,
  &quot;nextblockhash&quot;: &quot;00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048&quot;,
  &quot;strippedsize&quot;: 285,
  &quot;size&quot;: 285,
  &quot;weight&quot;: 1140,
  &quot;tx&quot;: [
    {
      &quot;txid&quot;: &quot;4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b&quot;,
      &quot;hash&quot;: &quot;4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b&quot;,
      &quot;version&quot;: 1,
      &quot;size&quot;: 204,
      &quot;vsize&quot;: 204,
      &quot;weight&quot;: 816,
      &quot;locktime&quot;: 0,
      &quot;vin&quot;: [
        {
          &quot;coinbase&quot;: &quot;04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73&quot;,
          &quot;sequence&quot;: 4294967295
        }
      ],
      &quot;vout&quot;: [
        {
          &quot;value&quot;: 50.00000000,
          &quot;n&quot;: 0,
          &quot;scriptPubKey&quot;: {
            &quot;asm&quot;: &quot;04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG&quot;,
            &quot;desc&quot;: &quot;pk(04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f)#vlz6ztea&quot;,
            &quot;hex&quot;: &quot;4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac&quot;,
            &quot;type&quot;: &quot;pubkey&quot;
          }
        }
      ],
      &quot;hex&quot;: &quot;01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000&quot;
    }
  ]
}
</code></pre>
<p>I see the <code>version</code> inside <code>tx</code> has the value <code>1</code>, so I understand the array of bytes should start with <code>00000001</code>, but on <a href="https://bitcoin.stackexchange.com/questions/2177/how-to-calculate-a-hash-of-a-tx">How to calculate a hash of a Tx?</a>, the accepted answer shows it starting with <code>01000000</code> and I don't understand why, could someone help please?</p>

CC BY-SA 4.0
2023-10-23T08:28:40.413Z
See [Transaction format](https://bitcoin.stackexchange.com/q/113631/13866)
RedGrittyBrick
<transaction-id><endianness><transaction-version>
When calculating the hash of transaction, why is the version used as "01000000" instead of "00000001"?
Pedro Martins Timóteo da Costa
# When calculating the hash of transaction, why is the version used as "01000000" instead of "00000001"?

(Asked by: username_0 on 2023-10-22)

According to [the protocol documentation for tx](https://en.bitcoin.it/wiki/Protocol_documentation#tx), the first field on the transaction data is a version number of size 4.

Based on this, I am trying to format the array of bytes needed to calculate the TX hash for the genesis TX, so I retrieve the block information from my node:

```
{
  "hash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
  "confirmations": 708859,
  "height": 0,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
  "time": 1231006505,
  "mediantime": 1231006505,
  "nonce": 2083236893,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "0000000000000000000000000000000000000000000000000000000100010001",
  "nTx": 1,
  "nextblockhash": "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048",
  "strippedsize": 285,
  "size": 285,
  "weight": 1140,
  "tx": [
    {
      "txid": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
      "hash": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b",
      "version": 1,
      "size": 204,
      "vsize": 204,
      "weight": 816,
      "locktime": 0,
      "vin": [
        {
          "coinbase": "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73",
          "sequence": 4294967295
        }
      ],
      "vout": [
        {
          "value": 50.00000000,
          "n": 0,
          "scriptPubKey": {
            "asm": "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f OP_CHECKSIG",
            "desc": "pk(04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f)#vlz6ztea",
            "hex": "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac",
            "type": "pubkey"
          }
        }
      ],
      "hex": "01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000"
    }
  ]
}

```

I see the `version` inside `tx` has the value `1`, so I understand the array of bytes should start with `00000001`, but on [How to calculate a hash of a Tx?](https://bitcoin.stackexchange.com/questions/2177/how-to-calculate-a-hash-of-a-tx), the accepted answer shows it starting with `01000000` and I don't understand why, could someone help please?

**Tags:** transaction-id, endianness, transaction-version

> **Comments:**
> **username_1**: See [Transaction format](https://bitcoin.stackexchange.com/q/113631/13866)  

---

## 2 answers

### Answer by username_3 on 2023-10-22. Score: 6 | Accepted answer

Because it's encoded as a [little endian](https://betterexplained.com/articles/understanding-big-and-little-endian-byte-order/) number, meaning the least significant byte goes first.

Example:

Number 1234 would be encoded as byte sequence `000004D2` when encoded as 4-byte big endian, and it would be encoded as byte sequence `D2040000` when encoded as 4-byte little endian.


---

### Answer by username_2 on 2023-10-22. Score: 1

bitcoin protocol use little endian order in it's byte array.


---

