Bitcoin Forum
November 18, 2019, 10:02:45 PM *
News: Help collect the most notable posts made over the last 10 years.
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Var length integers  (Read 160 times)
jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1554
Merit: 1352


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 25, 2019, 05:32:47 PM
Merited by MagicByt3 (1)
 #1

I made a thread on the bitcoin wiki section but it seems to have gone ignored. Maybe that board isn't as subscribed to as this one so here it goes:

From this page there's a table: https://en.bitcoin.it/wiki/Protocol_documentation#Message_types

valuestorage lengthformat
<0xFD1uint8_t
<=0xFFFF30xFD followed by the length as uint16_t
<=0xFFFFFFFF50xFE followed by length as uint64_t
-90xFF followed by length as uint64_t

There isn't really any description as to what the first character is supposed to represent? And why the second, third and forth are needed is this just in case the voltage gets misread?

The Bitcoin Forum is turning 10 years old! Join the community in sharing and exploring the notable posts made over the years.
1574114565
Hero Member
*
Offline Offline

Posts: 1574114565

View Profile Personal Message (Offline)

Ignore
1574114565
Reply with quote  #2

1574114565
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 1946
Merit: 2904


bc1qshxkrpe4arppq89fpzm6c0tpdvx5cfkve2c8kl


View Profile WWW
January 25, 2019, 05:52:14 PM
Merited by Foxpup (3), Jet Cash (3), Coding Enthusiast (1)
 #2

voltage?

This is just how you store integers. For integers that have a value less than 0xFD, you just have the integer as a single byte by itself. For an integer between 0xFD and 0xFFFF inclusive, you put the byte 0xFD followed by 2 bytes representing the integer. For integers between 0x010000 and 0xFFFFFFFF inclusive, you put 0xFE followed by 4 bytes representing the integer. Lastly for integers between 0x0100000000 and 0xFFFFFFFFFFFFFFFF, you put 0xFF followed by 8 bytes representing the integer.

jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1554
Merit: 1352


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 25, 2019, 06:06:01 PM
 #3

Aha that's quite simple! Sorry for the hassle.

Voltages as in baud.

Anything bigger than 64 bit uints are sent as strings I know that much then before this...

Coding Enthusiast
Hero Member
*****
Offline Offline

Activity: 701
Merit: 1164


Novice C♯ Coder


View Profile WWW
January 26, 2019, 07:13:26 AM
 #4

That part is in serious need of at least 2 or 3 examples, I have had trouble understanding it too. This page has an example which can help.
More:
integer|compact|Base16
_________________|_______________|____________________________
252|{ 252 }|{ 0xfc }
253|{ 253, 253, 0 }|{ 0xfd, 0xfd, 0x00 }
UInt16.MaxValue|{ 253, 255, 255 }|{ 0xfd, 0xff, 0xff }
UInt16.MaxValue + 1|{ 254, 0, 0, 1, 0 }|{ 0xfe, 0x00, 0x00, 0x01, 0x00 }


Projects List+Suggestion box
Donation link using BIP21
Bech32 Donation link!
BitcoinTransactionTool (0.9.2):  Ann - Source Code
Watch Only Bitcoin Wallet (supporting SegWit) (3.1.0):  Ann - Source Code
SharpPusher (broadcast transactions) (0.10.0): Ann - Source Code

darosior
Full Member
***
Offline Offline

Activity: 201
Merit: 242



View Profile WWW
January 26, 2019, 09:40:06 AM
 #5

Hi,

I had difficulties to understand it too the first time I read it. This page, written by in3rsha provides a good explanation about variable length integers (and on many other Bitcoin-related subjects).

jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1554
Merit: 1352


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 26, 2019, 01:22:10 PM
 #6

Do you pad out the front with zeros?

If you have 0xFC9782 does it become 0xFE00FC9782? I've seen some randomly complicated padding before so I'm just checking.

Eg. Software I was using stored hex in index orders [3][1][4][2][0][5] for some reason...

darosior
Full Member
***
Offline Offline

Activity: 201
Merit: 242



View Profile WWW
January 26, 2019, 02:48:42 PM
Last edit: January 26, 2019, 03:06:13 PM by darosior
Merited by ETFbitcoin (1), Coding Enthusiast (1)
 #7

Do you pad out the front with zeros?

If you have 0xFC9782 does it become 0xFE00FC9782? I've seen some randomly complicated padding before so I'm just checking.

Eg. Software I was using stored hex in index orders [3][1][4][2][0][5] for some reason...
No you dont. If you can read Python code here is an implementation which I find easy to understand : https://github.com/Crypto-lyon/genesis-parser/blob/c01167295e01570baf687c323ea6b7b311e5ba23/parser.py#L3. However the comments are in french because I made it for a french workshop, so feel free to ask question if something seems magic.

EDIT : The algorithm
Read the first byte from the file descriptor
If the first byte is less than 0xfd :
     The number is the first byte
Else if the first byte is equal to 0xfd:
     Read the second byte
     The number is the second byte
Else if the first byte is equal to 0xfe:
     Read the second and third bytes
     The number is the second and third bytes
Else if the first byte is equal to 0xff:
     Read the second third and fourth bytes, this is the number

Coding Enthusiast
Hero Member
*****
Offline Offline

Activity: 701
Merit: 1164


Novice C♯ Coder


View Profile WWW
January 26, 2019, 03:04:51 PM
Merited by ETFbitcoin (1)
 #8

It is not padding, you are representing an x-byte integer in little endian order. So for example when you have UInt16=253 it is actually 2 bytes in your memory (16-bit) and since it is little endian the least significant bit is at the beginning meaning { 253, 0 } and UInt16=256 would look like { 0, 1 } (256= 0*2560 + 1*2561)

If you have 0xFC9782 does it become 0xFE00FC9782?
The correct form is: fe8297fc00

Projects List+Suggestion box
Donation link using BIP21
Bech32 Donation link!
BitcoinTransactionTool (0.9.2):  Ann - Source Code
Watch Only Bitcoin Wallet (supporting SegWit) (3.1.0):  Ann - Source Code
SharpPusher (broadcast transactions) (0.10.0): Ann - Source Code

jackg
Copper Member
Legendary
*
Offline Offline

Activity: 1554
Merit: 1352


https://bit.ly/2FR9nyn - free python tutorials


View Profile
January 26, 2019, 04:55:51 PM
 #9

Silly me for not looking into little edian...

Do strings of numbers (like the public key) get represented in big or little edian?

Coding Enthusiast
Hero Member
*****
Offline Offline

Activity: 701
Merit: 1164


Novice C♯ Coder


View Profile WWW
January 26, 2019, 06:17:33 PM
 #10

Do strings of numbers (like the public key) get represented in big or little edian?
Public key's X and Y coordinates are represented as big endian.

Projects List+Suggestion box
Donation link using BIP21
Bech32 Donation link!
BitcoinTransactionTool (0.9.2):  Ann - Source Code
Watch Only Bitcoin Wallet (supporting SegWit) (3.1.0):  Ann - Source Code
SharpPusher (broadcast transactions) (0.10.0): Ann - Source Code

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!