struct. instance

Represents a struct instance created by new().

Example
const fmt = struct.new(…);

fmt.pack(…);

const values = fmt.unpack(…);

Methods

pack(…values) → {string}

Pack given values.

The pack() function creates a byte string containing the argument values packed according to the given format instance.

Returns the packed string.

Raises a runtime exception if a given argument value does not match the required type of the corresponding format string directive.

Parameters:
NameTypeDescription
values*(repeatable)

Variable number of values to pack.

Returns: string
Example
const fmt = struct.new(…);
const data = fmt.pack(…);

unpack(input, offsetopt) → {array}

Unpack given byte string.

The unpack() function interpretes a byte string according to the given format instance and returns the resulting values. If the optional offset argument is given, unpacking starts from this byte position within the input. If not specified, the start offset defaults to 0, the start of the given input string.

Returns an array of unpacked values.

Raises a runtime exception if an invalid input string or offset value is given.

Parameters:
NameTypeDescription
inputstring

The input string to unpack.

offsetnumber(optional, default: 0)

The offset within the input string to start unpacking from.

Returns: array
Example
const fmt = struct.new(…);
const values = fmt.unpack(…);