Skip to content

Core\Bytes::pack

Writes numbers and text into a buffer of bytes, in the layout you describe.

The first argument is the format. Each letter in it describes one field: how wide the field is, and for a number, which end of it comes first. The values after the format fill those fields in order. N writes a whole number in four bytes with the largest byte first, and V writes the same number with the smallest byte first. a, A and Z write text or bytes into a field of the width you give, and fill the rest of it with padding.

Every field states its own width, so two machines that agree on the format read the same bytes. A value that is too large for its field throws an error. It is never cut short and never wrapped around.

Good to know: the format and the values must match exactly. A field with no value left for it, and a value no field writes, both throw an error.

The examples below show the number fields first, then text fields and padding, then a complete message ready to send.

Parameters

  • $formatstringThe format string. It must be written directly in the code. A code may be followed by a count or *. For a number code, the count is how many values it takes. For a buffer code, it is the width in bytes.
  • ...$valuesmixedvariadicThe values to write: one per number field, and one buffer per buffer field. An integer field takes an int or a uint. A float field takes a float. No value is converted.

Return value

Returns bytes. The packed buffer.

Errors

  • RuntimeError$format has a code that is not in the table, has x*, or has a count larger than the biggest buffer. The codes s, S, i, I, l, L, q, Q, f, d, h, H, X and @ are not allowed. The error names what to use instead. A value is missing, has the wrong type, or does not fit the width of its field. A float is outside the binary32 range, or a buffer is wider than its field. Or the format uses fewer values than the call passes.

Examples

Writing number fields

<?nvs
// Shows how to write whole numbers into a buffer of bytes. The format string
// says how wide each field is, and which end of the number comes first.
// `N` writes a number in four bytes, largest byte first. This prints
// "0000045a".
echo Core\Encoding::toHex(Core\Bytes::pack('N', 1114)), "\n";
// `V` writes the same number in four bytes, smallest byte first. This prints
// "5a040000".
echo Core\Encoding::toHex(Core\Bytes::pack('V', 1114)), "\n";
// One format writes several fields in a row. `n` is two bytes and `C` is one,
// so this buffer is seven bytes long. This prints 7.
bytes $header = Core\Bytes::pack('NnC', 1114, 8080, 4);
echo Core\Bytes::length($header), "\n";
// A number that does not fit its field throws an error. `C` is one byte, so it
// holds 255 and nothing above it.
try {
echo Core\Encoding::toHex(Core\Bytes::pack('C', 256)), "\n";
} catch (Throwable $e) {
echo "256 does not fit in one byte\n";
}
Output
0000045a
5a040000
7
256 does not fit in one byte

Text fields and padding

<?nvs
// Shows how to write text into a field of a fixed width. The number after the
// letter is the width of the field in bytes.
// `a` fills the rest of the field with zero bytes. "Hi" in a field of four
// bytes prints "48690000".
echo Core\Encoding::toHex(Core\Bytes::pack('a4', 'Hi')), "\n";
// `A` fills the rest with spaces. This prints "48692020".
echo Core\Encoding::toHex(Core\Bytes::pack('A4', 'Hi')), "\n";
// `Z` keeps one byte of the field for a zero byte of its own, so "Hi" needs
// three bytes here. This prints "486900".
echo Core\Encoding::toHex(Core\Bytes::pack('Z3', 'Hi')), "\n";
// `x` writes one zero byte and takes no value. A count after it writes that
// many. This prints "0107000000".
echo Core\Encoding::toHex(Core\Bytes::pack('CCx3', 1, 7)), "\n";
// Text longer than its field throws an error. "Hi" needs two bytes, and this
// field holds one.
try {
echo Core\Encoding::toHex(Core\Bytes::pack('a1', 'Hi')), "\n";
} catch (Throwable $e) {
echo "the field is too small for \"Hi\"\n";
}
Output
48690000
48692020
486900
0107000000
the field is too small for "Hi"

Framing a message

<?nvs
// Shows how a program frames a message before it sends it. The reader at the
// other end needs the length of the message first, so it knows how much to
// read.
bytes $body = "{\"id\":1042}" as bytes;
// `N` writes the length in four bytes, and `a` with no width writes the whole
// of the next value. Both ends agree that the body starts at byte five.
bytes $frame = Core\Bytes::pack('Na', Core\Bytes::length($body), $body);
// The frame is the body and four bytes more. This prints 15.
echo Core\Bytes::length($frame), "\n";
// The first four bytes hold the length, which is 11 here. This prints
// "0000000b".
echo Core\Encoding::toHex(Core\Bytes::slice($frame, 0, 4)), "\n";
// The body follows them unchanged. This prints {"id":1042}.
echo Core\Bytes::slice($frame, 4) as string, "\n";
Output
15
0000000b
{"id":1042}