Documentation Home
MySQL Internals Manual


14.1.1.1 Integer Types

The MySQL Protocol has a set of possible encodings for integers:

  • Fixed-length integers

  • Length-encoded integers

14.1.1.1.1 Fixed-Length Integer Types
Protocol::FixedLengthInteger

A fixed-length integer stores its value in a series of bytes with the least significant byte first.

Example

A int<3> with the value 1 is stored as:

01 00 00
14.1.1.1.2 Length-Encoded Integer Type
Protocol::LengthEncodedInteger

An integer that consumes 1, 3, 4, or 9 bytes, depending on its numeric value

To convert a number value into a length-encoded integer:

  • If the value is < 251, it is stored as a 1-byte integer.

  • If the value is ≥ 251 and < (216), it is stored as fc + 2-byte integer.

  • If the value is ≥ (216) and < (224), it is stored as fd + 3-byte integer.

  • If the value is ≥ (224) and < (264) it is stored as fe + 8-byte integer.

Note

Up to MySQL 3.22, 0xfe was followed by a 4-byte integer.

To convert a length-encoded integer into its numeric value, check the first byte:

  • If it is < 0xfb, treat it as a 1-byte integer.

  • If it is 0xfc, it is followed by a 2-byte integer.

  • If it is 0xfd, it is followed by a 3-byte integer.

  • If it is 0xfe, it is followed by a 8-byte integer.

Caution

If the first byte of a packet is a length-encoded integer and its byte value is 0xfe, you must check the length of the packet to verify that it has enough space for a 8-byte integer.

If not, it may be an EOF_Packet instead.

Depending on the context, the first byte may also have other meanings:

Caution

0xff as the first byte of a length-encoded integer is undefined.

Implemented By

int<lenenc>

Example
fa       -- 250
fc fb 00 -- 251

User Comments
User comments in this section are, as the name implies, provided by MySQL users. The MySQL documentation team is not responsible for, nor do they endorse, any of the information provided here.
Sign Up Login You must be logged in to post a comment.