Atom

binary.unpack()

Reads a scalar or compound value from a packed byte span.

Signature

binary.unpack(descriptor, bytes[, order])

Parameters

Packable descriptor, packed span exactly the layout size, optional byte order.

Returns

Scalar with target representation or independent packed compound value.

Behavior

Uses the descriptor's offsets and byte order. Input is unchanged.

Errors

Wrong size, invalid scalar encoding, invalid view or order.

Example

Atom
use binary
assert(binary.unpack(u16, binary.unhex('0102'), 'big') == 258)

Read an integer from its bytes

Tell Atom the expected type and the order used to write it.

Atom
use binary
bytes = binary.unhex('0102')
print(binary.unpack(u16, bytes, 'big'))
Output
258

The big-endian bytes describe an unsigned 16-bit value.

Read a record from two bytes

Provide a layout that accounts for the whole buffer.

Atom
use binary
layout = binary.layout({x = u8, y = u8})
point = binary.unpack(layout, binary.unhex('0102'))
print(point.x, point.y)
Output
1 2

The returned record has its own storage.