2.1.1.6 LengthPrefixedString

The LengthPrefixedString represents a string value. The string is prefixed by the length of the UTF-8 encoded string in bytes. The length is encoded in a variable-length field with a minimum of 1 byte and a maximum of 5 bytes. To minimize the wire size, length is encoded as a variable-length field.


0


1


2


3


4


5


6


7


8


9

1
0


1


2


3


4


5


6


7


8


9

2
0


1


2


3


4


5


6


7


8


9

3
0


1

Length (variable)

...

String (variable)

...

Length (variable): A numerical value that can range from 0 to 2147483647 (2^31) inclusive.

To minimize the wire size, the encoding of the length MUST be encoded as follows:

  • The Length field MUST be at least 1 byte and MUST NOT be more than 5 bytes.

  • Each byte MUST hold the Length value in its lower 7 bits.

  • The high bit MUST be used to indicate that the length continues in the next byte.

  • In the case that all 5 bytes are used, the high 5 bits in the fifth byte MUST be 0.


    0


    1


    2


    3


    4


    5


    6


    7


    8


    9

    1
    0


    1


    2


    3


    4


    5


    6


    7


    8


    9

    2
    0


    1


    2


    3


    4


    5


    6


    7


    8


    9

    3
    0


    1

    Length_0-6

    A

Length_0-6 (7 bits): Length values range from 0 to 127 (7 bits).

A - Reserved_7 (1 bit): The value MUST be 0.


0


1


2


3


4


5


6


7


8


9

1
0


1


2


3


4


5


6


7


8


9

2
0


1


2


3


4


5


6


7


8


9

3
0


1

Length_0-6

A

Length_8-14

B

Length_0-6 (7 bits): Length values range from 128 to 16383 (14 bits).

A - Reserved_7 (1 bit): The value MUST be 1.

Length_8-14 (7 bits): Length values range from 128 to 16383 (14 bits).

B - Reserved_15 (1 bit): The value MUST be 0.


0


1


2


3


4


5


6


7


8


9

1
0


1


2


3


4


5


6


7


8


9

2
0


1


2


3


4


5


6


7


8


9

3
0


1

Length_0-6

A

Length_8-14

B

Length_16-22

C

Length_0-6 (7 bits): Length values range from 16384 to 2097151 (21 bits).

A - Reserved_7 (1 bit): The value MUST be 1.

Length_8-14 (7 bits): Length values range from 16384 to 2097151 (21 bits).

B - Reserved_15 (1 bit): The value MUST be 1.

Length_16-22 (7 bits): Length values range from 16384 to 2097151 (21 bits).

C - Reserved_23 (1 bit): The value MUST be 0.


0


1


2


3


4


5


6


7


8


9

1
0


1


2


3


4


5


6


7


8


9

2
0


1


2


3


4


5


6


7


8


9

3
0


1

Length_0-6

A

Length_8-14

B

Length_16-22

C

Length_24-30

D

Length_0-6 (7 bits): Length values range from 2097152 to 268435445 (28 bits).

A - Reserved_7 (1 bit): The value MUST be 1.

Length_8-14 (7 bits): Length values range from 2097152 to 268435445 (28 bits).

B - Reserved_15 (1 bit): The value MUST be 1.

Length_16-22 (7 bits): Length values range from 2097152 to 268435445 (28 bits).

C - Reserved_23 (1 bit): The value MUST be 1.

Length_24-30 (7 bits): Length values range from 2097152 to 268435445 (28 bits).

D - Reserved_31 (1 bit): The value MUST be 0.


0


1


2


3


4


5


6


7


8


9

1
0


1


2


3


4


5


6


7


8


9

2
0


1


2


3


4


5


6


7


8


9

3
0


1

Length_0-6

A

Length_8-14

B

Length_16-22

C

Length_24-30

D

Length_32-38

E

Length_0-6 (7 bits): Length values range from 268435456 to 2147483647 (31 bits).

A - Reserved_7 (1 bit): The value MUST be 1.

Length_8-14 (7 bits): Length values range from 268435456 to 2147483647 (31 bits).

B - Reserved_15 (1 bit): The value MUST be 1.

Length_16-22 (7 bits): Length values range from 268435456 to 2147483647 (31 bits).

C - Reserved_23 (1 bit): The value MUST be 1.

Length_24-30 (7 bits): Length values range from 268435456 to 2147483647 (31 bits).

D - Reserved_31 (1 bit): The value MUST be 1.

Length_32-38 (7 bits): Length values range from 268435456 to 2147483647 (31 bits).

E - Reserved_39 (1 bit): The value MUST be 0.

String (variable): A UTF-8 encoded string value. The number of bytes of the encoded string MUST be equal to the value specified in the Length field.