This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Imagine for a moment you were just hired by an IoT developer. To facilitate variable length packets the IoT MQTT protocol defines something called a Variable Length Integer.
The Variable Length Integer is encoded using an encoding scheme which uses a single byte for values up to 127. Larger values are handled as follows:
The least significant seven bits of each byte encode the data, and the most significant bit is used to indicate whether there are bytes following in the representation. Thus, each byte encodes 128 values (0-127) and a "continuation bit".
The maximum number of bytes in the Variable Byte Integer field is four.
The encoded value MUST use the minimum number of bytes necessary to represent the value
- Your assignment is to write a function to read a variable length integer from the stream and return it's value as an integer. The integer should not exceed 32 bits, or an exception should be indicated.
- Additionally you need to write a procedure that when passed an integer, will encode it as a variable length integer and send it to the stream.
EDIT: to add:
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)
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/dailyprogra...