Skip to content

struct

The struct module provides binary data serialization and memory alignment utilities, allowing RayQuiro to pack variables into contiguous binary byte buffers and unpack binary data received from C libraries, network sockets, or binary file formats.


SpecifierC TypeSize (Bytes)Range
bint8_t1-128 .. 127
Buint8_t10 .. 255
hint16_t2-32,768 .. 32,767
Huint16_t20 .. 65,535
i, lint32_t4-2,147,483,648 .. 2,147,483,647
I, Luint32_t40 .. 4,294,967,295
qint64_t8-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807
Quint64_t80 .. 18,446,744,073,709,551,615
ffloat4IEEE-754 32-bit single precision
ddouble8IEEE-754 64-bit double precision
pintptr_t8Pointer / buffer address

FunctionSignatureReturn TypeDescription
struct.calcsizestruct.calcsize(format)numberCalculates total byte size required by the format string.
struct.packstruct.pack(format, ...values)BufferPacks arguments into a contiguous binary byte buffer.
struct.unpackstruct.unpack(format, buffer, [offset])arrayUnpacks values from a buffer according to the format string.

var size = struct.calcsize("b h i d")
print("Struct size: " + size)
var buf = struct.pack("b h i d", 1, 500, 100000, 3.14159)
var values = struct.unpack("b h i d", buf, 0)
print(values[0])
print(values[1])
print(values[2])
print(values[3])
var rect = struct.pack("i i i i", 10, 20, 800, 600)
var lib = ffi.load("user32.dll")
ffi.close(lib)