Bitcoin Forum
June 26, 2019, 05:20:12 PM *
News: Latest Bitcoin Core release: 0.18.0 [Torrent] (New!)
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Var length integers  (Read 152 times)
jackg
Copper Member
Legendary
*
Online Online

Activity: 1414
Merit: 1268


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?

1561569612
Hero Member
*
Offline Offline

Posts: 1561569612

View Profile Personal Message (Offline)

Ignore
1561569612
Reply with quote  #2

1561569612
Report to moderator

Mine RVN and with 0% mining fees and get paid in BTC, ETH, XMR or RVN.

www.cudominer.com Get Cudo Miner
Auto coin switching, third-party miners, overclocking and remote management (Win/Linux)
Run from a USB stick or install from an ISO image (Linux)
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1561569612
Hero Member
*
Offline Offline

Posts: 1561569612

View Profile Personal Message (Offline)

Ignore
1561569612
Reply with quote  #2

1561569612
Report to moderator
1561569612
Hero Member
*
Offline Offline

Posts: 1561569612

View Profile Personal Message (Offline)

Ignore
1561569612
Reply with quote  #2

1561569612
Report to moderator
1561569612
Hero Member
*
Offline Offline

Posts: 1561569612

View Profile Personal Message (Offline)

Ignore
1561569612
Reply with quote  #2

1561569612
Report to moderator
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 1806
Merit: 2482


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
*
Online Online

Activity: 1414
Merit: 1268


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: 636
Merit: 887


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: 170
Merit: 185



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).

Github profile ~ Crypto Lyon (french community)  ~ GPG key fingerprint : E13F C145 CD3F 430 ~ LN public key : 03678b4f041fbfbeebcafc076469df75decf81da20a53bd490172e83ce532df1fa
Tutorials/posts : Address from private keyBitcoin smart contracts ~ Setup a Bitcoin node ~ Raw P2PKH transaction in Python ~ P2SH ~ 51% attack
jackg
Copper Member
Legendary
*
Online Online

Activity: 1414
Merit: 1268


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: 170
Merit: 185



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

Github profile ~ Crypto Lyon (french community)  ~ GPG key fingerprint : E13F C145 CD3F 430 ~ LN public key : 03678b4f041fbfbeebcafc076469df75decf81da20a53bd490172e83ce532df1fa
Tutorials/posts : Address from private keyBitcoin smart contracts ~ Setup a Bitcoin node ~ Raw P2PKH transaction in Python ~ P2SH ~ 51% attack
Coding Enthusiast
Hero Member
*****
Offline Offline

Activity: 636
Merit: 887


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
*
Online Online

Activity: 1414
Merit: 1268


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: 636
Merit: 887


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!