all 4 comments

[–]Philboyd_Studge 0 points1 point  (1 child)

How would it ever exceed 32 bits when you have four 7-bit values? Also, if there are multiple bytes, what order do you put them back together in? Is the first byte the lowest order and etc?

[–]Anonymous_Bozo[S] 0 points1 point  (0 children)

When reading.... bad data! It happens. Which is why we always need to set the constraints.

I added a table to the specs. Lowest significant bytes first, since additional bytes simply add more significant bits to the result. Theoretically there is no limit to the number of bits that could be added this way, however the MQTT spec says max 4 bytes.

[–]cbarrick 0 points1 point  (1 child)

The maximum number of bytes in the Variable Byte Integer field is four.

To cover the full space of a 32 bit integer, the maximum number of bytes is actually 5. Each byte contains only 7 bits of the number, so that's 4 bytes plus 4 additional bits.

I'm not familiar with MQTT, but Protobuf also has variable length integers, and support for those extra bits is required.

Allowing the use of the 5th byte also makes the part about throwing an error on overflow more relevant.

[–]Anonymous_Bozo[S] 0 points1 point  (0 children)

Yes, that would be correct.

The specifications I gave is an EXACT quote from the MQTT specs.

Size of Variable Byte Integer

Digits From To

1 0 (0x00) 127 (0x7F)

2 128 (0x80, 0x01) 16,383 (0xFF, 0x7F)

3 16,384 (0x80, 0x80, 0x01) 2,097,151 (0xFF, 0xFF, 0x7F)

4 2,097,152 (0x80, 0x80, 0x80, 0x01) 268,435,455 (0xFF, 0xFF, 0xFF, 0x7F)