util.string.packsize
Shared
Retrieves the size of a string resulting from `util.string.pack`
Syntax
local size = util.string.packsize(
format
)Parameters
| Type | Name | Description |
|---|---|---|
string | format | Format string containing specifiers describing how to pack the values Refer Specifiers section |
Returns
| Type | Name | Description |
|---|---|---|
int | size | The size in bytes of the packed string that would result from util.string.pack |
Specifiers
| Specifier | Description |
|---|---|
b | Signed byte (char) |
B | Unsigned byte (unsigned char) |
h | Signed short |
H | Unsigned short |
l | Signed long |
L | Unsigned long |
j | lua_Integer |
J | lua_Unsigned |
T | size_t |
i[n] | Signed int with n bytes |
I[n] | Unsigned int with n bytes |
f | Float |
d | Double |
n | Lua number |
c[n] | Fixed-size string of n bytes |
z | Zero-terminated string |
s[n] | String preceded by length |
x | One byte of padding |
< | Set little endian |
> | Set big endian |
= | Set native endian |
Examples
local size = util.string.packsize("iii")
core.engine.print("info", size) -- 12local size = util.string.packsize("d")
core.engine.print("info", size) -- 8local size = util.string.packsize("c10")
core.engine.print("info", size) -- 10local size = util.string.packsize("ifd")
core.engine.print("info", size) -- 16local size = util.string.packsize("<I4")
core.engine.print("info", size) -- 4local size = util.string.packsize("b")
core.engine.print("info", size) -- 1local size = util.string.packsize("h")
core.engine.print("info", size) -- 2local size = util.string.packsize("l")
core.engine.print("info", size) -- 8local size = util.string.packsize("ixxi")
core.engine.print("info", size) -- 12core.engine.print("info", "i4:", util.string.packsize("i4")) -- 4
core.engine.print("info", "i8:", util.string.packsize("i8")) -- 8local fmt = "i4i4f"
local bufferSize = util.string.packsize(fmt)
core.engine.print("info", "Need", bufferSize, "bytes") -- 'Need 12 bytes'local size = util.string.packsize("c5c5c5")
core.engine.print("info", size) -- 15local fmt = "ifd"
local size = util.string.packsize(fmt)
local packed = util.string.pack(fmt, 1, 2.5, 3.14)
core.engine.print("info", #packed == size) -- truelocal recordFormat = "c32i4i4"
local recordSize = util.string.packsize(recordFormat)
core.engine.print("info", "Each record:", recordSize, "bytes")local headerFormat = ">I2I2I4"
local headerSize = util.string.packsize(headerFormat)
core.engine.print("info", "Header size:", headerSize) -- 8