The sections contains explanations and additional information to the Table 20.1 . The format specifier rX, which means a lower case r followed by a number, serves the purpose of skipping bits but does not read or write any data. Therefore the nature of this format is different from any other format and no Lua type is associated to this format. Skipping of bits is only needed if more than one format is used in a row which have all bit alignment.
An octet is a set of 8 bits which is often also described as a byte. Bit alignment means that the format ”bbn” will read/write 6 bits inside of 1 octet. These formats read/write a stream of bits. If 8 bits are written, writing just continues, without respect to octet borders. So a nibble (4 bits) can be split across 2 octets, depending on how many bits have been read/written before.
Octet alignment is different. The format ”u12u12” describes 24 bits of data and could be in theory written into 3 octets if u12 were bit aligned (”nnnnnn”3 octets). But as u12 is byte aligned the second 12 bit integer will read from/write into a new octet so that 4 octets are used.
Now what does the format ”u12n” do? An octet-aligned format enforces octet alignment surrounding itself - it reads/writes on the next octet border and enforces a potentially following format on a octet border as well.
The Bluetooth ® LE specification allows exponents for integer types (1.2 ∗ 10exponent). The exponent is not encoded inside the data itself but needs to be used on encoding and decoding of the data. This concept is known as fixed-point number format. If an exponent is allowed for a format but no exponent is specified it defaults to 0 (100). if an exponent is specified read/write of the format will apply the exponent automatically (”u8e1u8e−1s16e−7”). Simply add a lower case e followed by a number after the format specifier.
Table 20.1: Format specifier for pack and unpack
Format Specifier |
DataType Description |
Lua Type |
Alignment |
Exponent |
---|---|---|---|---|
rX |
X number of bits skipped |
N/A |
bit |
N/A |
b |
boolean |
boolean |
bit |
No |
b2 |
2 bits |
number |
bit |
No |
n |
4 bits (nibble) |
number |
bit |
No |
u8 |
unsigned integer 8 bits |
number |
octet |
Yes |
u12 |
unsigned integer 12 bits |
number |
octet |
Yes |
u16 |
unsigned integer 16 bits |
number |
octet |
Yes |
u24 |
unsigned integer 24 bits |
number |
octet |
Yes |
u32 |
unsigned integer 32 bits |
number |
octet |
Yes |
u48 |
unsigned integer 48 bits |
number |
octet |
Yes |
s8 |
signed integer 8 bits |
number |
octet |
Yes |
s12 |
signed integer 12 bits |
number |
octet |
Yes |
s16 |
signed integer 16 bits |
number |
octet |
Yes |
s24 |
signed integer 24 bits |
number |
octet |
Yes |
s32 |
signed integer 32 bits |
number |
octet |
Yes |
s48 |
signed integer 48 bits |
number |
octet |
Yes |
f |
IEEE-754 32-bit floating point |
number |
octet |
No |
fl |
IEEE-754 64-bit floating point |
number |
octet |
No |
S8 |
UTF-8 string |
string |
octet |
No |
S16 |
UTF-16 string |
string |
octet |
No |