Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length-Value encoding

Tags:

encode

I have a homework question. I do not understand the example given. Because how can a byte be 255 bytes? I thought that, because of this example, the answer to (a) is 510 bytes, and the answer of (b) is 128. But yeah, i am confused.. Can someone maybe explain this one?

An alternative encoding scheme is Length-Value encoding, where each string is encoded as a sequence of two elements:  L: a fixed length value that speci fies the length of the string in bytes  V: the string itself The length of L, however, limits the maximum length of V. For example if L is a byte V cannot be longer than 255 bytes. Knowing this limitation, how many values can be injectively encoded using LV encoding (a) if the length is encoded using 2 bytes? (b) if the length is encoded using Length-Value encoding with length of 4 bits?

like image 813
mathew Avatar asked Dec 06 '25 19:12

mathew


1 Answers

It is not saying that a byte is 255 bytes. It is saying that V (the string being encoded) cannot be longer than 255 bytes, if its length (L) is specified by a single byte 2^8 (0-255). If you used more two bytes you would be able to specify strings up to 2^16 (0-65535) characters long.

For example, if we wanted to encode the value Example. We would write its length (L) and then the value (V). I.e. we would write 7 as a byte followed by the bytes for the ASCII values of the characters in the word 'E', 'x', 'a', 'm', 'p', 'l', 'e'. If we only use a byte for the length then we cannot encode a very long string because a byte's range is 0-255 or 1-256 (256 = 2^8) because a byte only contains 8 bits.

The answers it wants are (a) (2^16 - 1) = 65535 and (b) (2^4 - 1) = 15.

However, the question is mistaken that the maximum length is 255. It should be 256, unless you want to be able to specify zero length strings.

like image 194
ᴇʟᴇvᴀтᴇ Avatar answered Dec 11 '25 13:12

ᴇʟᴇvᴀтᴇ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!