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

15.5 unpack

..., remnant = string.unpack("formatString", characteristicValue)

Unpacks a Bluetooth ® LE characteristic data value into one or multiple Lua values. The number of returned values is defined by the format specifiers inside the formatString. All supported format specifiers are listed 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 unpacked

characteristicValue

in string

The characteristic value read.

...

out any

The parameter list associated to the format specified

remnant

out string

The remnant of the characteristicValue if the format did not decode all data, or nil otherwise.

Listing 15.4: Example Showing the use of string.unpack()

bool1, number, bool2 = string.unpack("bb2b", data)
bool1, bool2, bool3 = string.unpack("bbr2b", data)

Similar to the pack function it is possible to split the unpacking of the data into multiple calls to unpack. This can be achieved by passing the remnant returned of one call to unpack as characteristic value to the next call of unpack. Listing 15.5 show the two scenarios.

Listing 15.5: Splitting Unpacking into Multiple calls to string.unpack()

ten, twelve = string.unpack('u8u8', '\\10\\12')
ten, remnant = string.unpack('u8', '\\10\\12') -- returns 10, '\\12'
twelve = string.unpack('u8', remnant) -- returns 12, nil

Introduced in platform.apiLevel = '2.7'