Bitcoin Forum
July 07, 2020, 01:49:01 PM *
News: Latest Bitcoin Core release: 0.20.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Block variable length integers?  (Read 928 times)
DannyHamilton
Legendary
*
Offline Offline

Activity: 2338
Merit: 1728



View Profile
December 12, 2012, 11:18:14 PM
 #1

I'm never sure if questions like this belong in "Technical Support" or "Technical Discussion".

Anyhow, I was looking at this website trying to get a better understanding of the blocks in the blockchain:

http://james.lab6.com/2012/01/12/bitcoin-285-bytes-that-changed-the-world

I saw that the count of transactions, count of transaction inputs, response script length, number of outputs, and challenge script length are all "variable length integers":

There doesn't seem to be any "size" field before the integer to indicate the length, so if I were to try to create a program that parses the blockchain, how do I know how many bytes to parse out to determine the number of transactions?



1594129741
Hero Member
*
Offline Offline

Posts: 1594129741

View Profile Personal Message (Offline)

Ignore
1594129741
Reply with quote  #2

1594129741
Report to moderator
1594129741
Hero Member
*
Offline Offline

Posts: 1594129741

View Profile Personal Message (Offline)

Ignore
1594129741
Reply with quote  #2

1594129741
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
DannyHamilton
Legendary
*
Offline Offline

Activity: 2338
Merit: 1728



View Profile
December 12, 2012, 11:51:49 PM
 #2

Never mind.  I found my answer.  Now to figure out the code to convert it...

Quote
. . . essentially a base-128 representation of an unsigned integer with the addition of the eighth bit to mark continuation of bytes . . . The encoding assumes an octet (an eight-bit byte) where the most significant bit (MSB), also commonly known as the sign bit, is reserved to indicate whether another variable length integer octet follows . . .

Here is a worked out example for the decimal number 137:

  • Represent the value in binary notation (e.g. 137 as 10001001)
  • Break it up in groups of 7 bits starting from the lowest significant bit (e.g. 137 as 0000001 0001001). This is equivalent to representing the number in base 128.
  • Take the lowest 7 bits and that gives you the least significant byte (0000 1001). This byte comes last.
  • For all the other groups of 7 bits (in the example, this is 000 0001), set the MSB to 1 (which gives 1000 0001 in our example). Thus 137 becomes 1000 0001 0000 1001 where the bits in boldface are something we added. These added bits denote if there is another byte to follow or not. Thus, by definition, the very last byte of a variable length integer will have 0 as its MSB.

Another way to look at this is to represent the value in base-128, and then set the MSB of all but the last base-128 digit to 1.

Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!