unpack

Syntax

unpack(format, buf)

Details

Unpack from the buf according to the format string specified by format. The result is a tuple with the unpacked data even if it contains exactly one item.

Parameters

format is a format string.
  • A format character may be preceded by an integral repeat count. For example, the format string '4h' means exactly the same as 'hhhh'.

  • Whitespace characters between formats are ignored; a count and its format must not contain whitespace though.

  • For the 's' format character, the count is interpreted as the length of the bytes, not a repeat count like for the other format characters; for example, '10s' means a single 10-byte string, while '10c' means 10 characters. If a count is not given, it defaults to 1. The string is truncated or padded with null bytes as appropriate to make it fit.

buf is a bytes object of STRING or BLOB type. The size of buf in bytes must match the size required by the format.

Returns

It returns a tuple with the unpacked data.

Examples

res = pack("N",1);
res1 = unpack("N", res);
print(res1)
// output: (1)

res = pack("3s i", `123, 3)
res1 = unpack("3s i",  res);
print(res1)
// output: ("123",3)

res = pack("3s i", `123, 3)
res2 = unpack("3s i",  blob(res));
print(res2)
// output: ("123",3)

Related function: pack

Appendix

Format Characters

Format mapping:

Format C Type Python Type DolphinDB Type Range
x pad byte no value VOID
c char bytes of length 1 CHAR -27 +1~27 -1
b signed char integer LONG -27 ~27 -1
B unsigned char integer LONG 0~28 -1
? _Bool bool LONG -263 ~263 -1
h short integer LONG -215 ~215 -1
H unsigned short integer LONG 0~216 -1
i int integer LONG -231 ~231 -1
I unsigned int integer LONG 0~232 -1
l long integer LONG -231~231 -1
L unsigned long integer LONG 0~232 -1
q long long integer LONG -263 ~263 -1
Q unsigned long long integer LONG 0~263 -1
n ssize_t integer LONG -263 ~263 -1
N size_t integer LONG 0~263 -1
f float float LONG -3.40E+38 ~ +3.40E+38
d double float LONG -1.79E+308 ~ +1.79E+308
s char[] bytes STRING
p char[] bytes STRING
P void* integer LONG -263 ~263 -1

Byte Order, Size, and Alignment

Character Byte order Size Alignment
> big-endian standard none
= native standard none
< little-endian standard none
@ native native native
! network
(= big-endian) native none

The first character of the format string can be used to indicate the byte order, size and alignment of the packed data, see "Appendix: Byte Order, Size and Alignment". If the first character is not one of these characters, '@' is assumed.