# How happens the serealization the data in the file blkxxxx.dat in the bitcoin

I'm writing a blockchain parser for bitcoin core with C++ and now I want to enrich the data I read with the raw transaction and the previous and next block hashes.

I used this library to convert the hexadecimal to a double sha256 but now my problem is to bring the data I read into the correct format, for example, the VarInt is serialized as corresponding hexadecimal (for example 77 -> 4d). But on some things, I don't find myself, for example in the conversion of integers of the type uint32 and int32.

I'll give you an example The version number of the genesis block is 1 and the serialization to the correct hash is 01000000

or the conversion of the nonce of the genesis block which is 2083236893 but the correct form for the serialization is 7c2bac1d

Similarly for

NBits = 486604799 -> serialization = 1d00ffff timestamp = 1231469665 -> serialization = 495fab29

Sorry for my terrible English but I'm learning.

______ Solution ______

Bitcoin core to use the method for serializing block, into file blk and sending data into the network, to create the hash.

Then, the hash block do create with all data into little endian form.

## 1 Answer

You are being confused three ways.

First, you need to be careful that some of your numbers in decimal, others are in hexadecimal: 77 decimal -> 4d hexadecimal (so: 77d -> 0x4d)

Second, you need to be careful that most numbers are stored in little endian notation, so 0x00000001 -> 0x01000000. Theymos' discussion why little endian is used.

Third, you have mixed up numbers from the Genesis Block and those from block 125552:

The Genesis block nonce 2083236893d converts to 0x7c2bac1d as hexadecimal not 0x42a14695.

0x42a14695 is the nonce mentioned at https://en.bitcoin.it/wiki/Block_hashing_algorithm for Block 125552, not for the Genesis block.

Similarly, Nbits 486604799d is for the Genesis block, 0xf2b9441a is for block 125552. Timestamp 1231469665d is for the Genesis block, 0xc7f5d74d is for block 125552.

• And what the extreme programming advocates say is true, programming in pairs greatly reduces errors, fortunately I had the test battery well designed and I only had to change the asserts, thank you so much for taking me on the right track. For the hexadecimal convention, I apologize for my mistake. Now I would like to ask you a question, why is the nonce stored as endian bih and the versions as endian lite? there is some reference of the protocol where we talk about this to understand how to correctly convert the fields Mar 30 '19 at 14:42
• Nonce and versions are both stored as little-endian. So, the Genesis block nonce 2083236893d is 0x7c2bac1d, which is changed to its little-endian form 0x1dac2b7c before being stored. Mar 30 '19 at 16:04
• Thanks so much, bitcoin create the hash whit all type little endian :) If you would not have helped me I could not solve the problem Apr 5 '19 at 20:41