You are here: Lua Scripting Resources eGuide > Lua Scripting API Reference Guide > String Library Extension > pack

15.4 pack

characteristicValue = string.pack("formatString", ...)

Packs one or multiple Lua values into a Bluetooth ® LE characteristic data value. The number or arguments after the formatString must match the number of formats specified inside the formatString. The format specifier used to build the formatString as specified in Table 20.1 and additional details can be found in subsection 20.1.5.

Parameter

Type

Description

"formatString"

in string

Lists one or multiple formats to be packed

...

in any

The parameter list associated to the format specified

characteristicValue

out string

The packed characteristic value to be written.

Listing 15.2: Example Showing the use of string.pack()

data  = string.pack("bb2b", true, 2,  false) -- binary data 1100
data = string.pack("bbr2b", true, false, true) -- binary data 10001

If the format is complex and repetitively used across multiple characteristic values it it is possible to split the packing of the data into multiple calls to pack. Combining the multiple pack results into one piece of data can be achieved by string concatenation. Listing 15.3 shows two simple
lines which result in the same data value.

Listing 15.3: Concatenation of Multiple calls to string.pack()

data1 = string.pack('u8u8', 10, 12)
data2 = string.pack('u8', 10) .. string.pack('u8', 12)

Introduced in platform.apiLevel = '2.7'