Core\Bytes::unpack
Core\Bytes::unpack(bytes $b, string $format): array<mixed>Core\Bytes::unpack( bytes $b, string $format, ): array<mixed>
Reads a buffer of bytes back into numbers and text, following the same format that Core\Bytes::pack writes.
The buffer comes first and the format second. Each letter in the format describes one field, and the result is a list with one entry per field, in the order the format names them. The list is positional, so the first field is at position 0. A number code gives a number, and a text code gives a bytes value with its own padding taken off.
The format must describe the whole buffer. Bytes left over after the last field throw an error, and so does a field that reaches past the end of the buffer. To read a header out of a longer buffer, cut the header off first with Core\Bytes::slice.
Good to know: the two members share one format. Reading a buffer with the format that wrote it gives the same values back, field for field.
The examples below read number fields first, then text fields and their padding, then a message that arrived over a socket.
Parameters
$bbytesThe buffer to read.$formatstringThe same format string thatpacktakes. It must be written directly in the code. Fields have no names, so the result is a list.
Return value
Returns array<mixed>. The fields as a list, in order. A c field gives an int, and every other integer code gives a uint. G, g, E and e give a float. A buffer code gives bytes, with the padding of A and Z removed.
Errors
RuntimeError$formathas a code that is not in the table, or a count larger than the biggest buffer. Or a field reads past the end of$b, or bytes are left after the last field.
Examples
Reading number fields
<?nvs// Shows how to read whole numbers out of a buffer of bytes. The format says how// wide each field is, and which end of the number comes first.
// `N` reads four bytes as one number, largest byte first. This prints 1114.array<mixed> $one = Core\Bytes::unpack(Core\Encoding::fromHex('0000045a'), 'N');echo ($one[0] as uint) as string, "\n";
// `V` reads the same four bytes smallest byte first, which is a different// number. This prints 1510211584.array<mixed> $other = Core\Bytes::unpack(Core\Encoding::fromHex('0000045a'), 'V');echo ($other[0] as uint) as string, "\n";
// One format reads several fields, and the result is a list in the order of the// format. This prints 1114 8080 4.array<mixed> $header = Core\Bytes::unpack(Core\Bytes::pack('NnC', 1114, 8080, 4), 'NnC');echo ($header[0] as uint) as string, ' ', ($header[1] as uint) as string, ' ', ($header[2] as uint) as string, "\n";
// The list holds one entry per field. This prints 3.echo Core\Arr::count($header), "\n";111415102115841114 8080 43Text fields and padding
<?nvs// Shows how to read text fields back. Each of the three text codes does// something different with the padding around the text.
bytes $record = Core\Bytes::pack('a6A6Z6', 'Hi', 'Hi', 'Hi');array<mixed> $fields = Core\Bytes::unpack($record, 'a6A6Z6');
// `a` gives the whole field. The four zero bytes are still in it, so this// prints "486900000000".echo Core\Encoding::toHex($fields[0] as bytes), "\n";
// `A` takes the spaces off the end of the field. This prints "4869".echo Core\Encoding::toHex($fields[1] as bytes), "\n";
// `Z` stops at the first zero byte. This prints "4869".echo Core\Encoding::toHex($fields[2] as bytes), "\n";
// `x` steps over bytes and makes no field, so the number is the only entry in// this list. This prints 1 and 7.array<mixed> $skipped = Core\Bytes::unpack(Core\Encoding::fromHex('00000007'), 'x3C');echo Core\Arr::count($skipped), ' ', ($skipped[0] as uint) as string, "\n";486900000000486948691 7Reading a framed message
<?nvs// Shows how a program reads a message that arrived over a socket. The first four// bytes hold the length of the body, so the reader takes those bytes first and// then reads exactly that many more.
bytes $frame = Core\Bytes::pack('Na', 11, "{\"id\":1042}" as bytes);
// The format must describe the whole buffer, so cut the first four bytes off and// read the length out of them. This prints 11.array<mixed> $head = Core\Bytes::unpack(Core\Bytes::slice($frame, 0, 4), 'N');int $length = $head[0] as int;echo $length, "\n";
// The body is that many bytes after the first four. This prints {"id":1042}.echo Core\Bytes::slice($frame, 4, $length) as string, "\n";
// A buffer you already hold whole needs one call: `N` for the length, and `a`// with no width for all the rest. This prints 11 and {"id":1042}.array<mixed> $both = Core\Bytes::unpack($frame, 'Na');echo ($both[0] as uint) as string, ' ', ($both[1] as bytes) as string, "\n";11{"id":1042}11 {"id":1042}