pack - Packing and unpacking format string#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

There are also video tutorials in this library, click this link to view

Example

--[[
 '<' Set to Little Ended 
 '>' Set to Big Endian 
 '=' Size side follows local settings 
 'z' Empty string, 0 bytes
 'a' size_t String, first 4 bytes express length, then N bytes of data
 'A' Specifies the length string, such as A8, which represents 8 bytes of data.
 'f' float, 4 Byte
 'd' double , 8 Byte
 'n' Lua number , 32bit Firmware 4 bytes, 64bit firmware 8 bytes
 'c' char , 1 Byte
 'b' byte = unsigned char  , 1 Byte
 'h' short  , 2 Byte
 'H' unsigned short  , 2 Byte
 'i' int  , 4 Byte
 'I' unsigned int , 4 Byte
 'l' long , 8 Bytes, only 64bit firmware can correctly obtain
 'L' unsigned long , 8 Bytes, only 64bit firmware can correctly obtain
]]

-- Please see the detailed usage demo

pack.unpack( string, format, init)#

Unpack String

Parameters

Incoming Value Type

Explanation

string

String to be unpacked

string

Formatting Symbols

int

The default value is 1, which marks where unpacking begins

Return Value

return value type

explanation

int

Position of string marker

any

The first unpacked value may have N return values according to the format value.

Examples

local _,a = pack.unpack(x,">h") --Unpack into short (2 bytes)

pack.pack( format, val1, val2, val3, valn )#

value of the packed string

Parameters

Incoming Value Type

Explanation

string

format Formatting Symbols

any

First value to pack

any

Second value to pack

any

Second value to pack

any

nth value to be packed

Return Value

return value type

explanation

string

A string containing all the formatting variables

Examples

local data = pack.pack('<h', crypto.crc16("MODBUS", val))
log.info("data", data, data:toHex())