Linux and UNIX Man Pages

Linux & Unix Commands - Search Man Pages

unpack(3) [php man page]

UNPACK(3)								 1								 UNPACK(3)

unpack - Unpack data from binary string

SYNOPSIS
array unpack (string $format, string $data) DESCRIPTION
Unpacks from a binary string into an array according to the given $format. The unpacked data is stored in an associative array. To accomplish this you have to name the different format codes and separate them by a slash /. If a repeater argument is present, then each of the array keys will have a sequence number behind the given name. PARAMETERS
o $format - See pack(3) for an explanation of the format codes. o $data - The packed data. RETURN VALUES
Returns an associative array containing unpacked elements of binary string. CHANGELOG
+--------+---------------------------------------------------+ |Version | | | | | | | Description | | | | +--------+---------------------------------------------------+ | 5.5.0 | | | | | | | Changes were made to bring this function into | | | line with Perl: The "a" code now retains trail- | | | ing NULL bytes. The "A" code now strips all | | | trailing ASCII whitespace (spaces, tabs, new- | | | lines, carriage returns, and NULL bytes). The | | | "Z" code was added for NULL-padded strings, and | | | removes trailing NULL bytes. | | | | +--------+---------------------------------------------------+ EXAMPLES
Example #1 unpack(3) example <?php $binarydata = "x04x00xa0x00"; $array = unpack("cchars/nint", $binarydata); ?> The resulting array will contain the entries "chars" with value 4 and "int" with 160. Example #2 unpack(3) example with a repeater <?php $binarydata = "x04x00xa0x00"; $array = unpack("c2chars/nint", $binarydata); ?> The resulting array will contain the entries "chars1", "chars2" and "int". NOTES
Caution Note that PHP internally stores integral values as signed. If you unpack a large unsigned long and it is of the same size as PHP internally stored values the result will be a negative number even though unsigned unpacking was specified. Caution Be aware that if you do not name an element, an empty string is used. If you do not name more than one element, this means that some data is overwritten as the keys are the same such as in: Example #3 unpack(3) example with unnamed keys <?php $binarydata = "x32x42x00xa0"; $array = unpack("c2/n", $binarydata); var_dump($array); ?> The resulting array will contain the entries "1" with value 160 and "2" with 66. The first value from the c specifier is overwritten by the first value from the n specifier. SEE ALSO
pack(3). PHP Documentation Group UNPACK(3)

Check Out this Related Man Page

UNPACK(3PVM)							  PVM Version 3.4						      UNPACK(3PVM)

NAME
pvm_unpack - Unpack the active message buffer into arrays of prescribed data type. SYNOPSIS
C int info = pvm_unpackf( const char *fmt, ... ) int info = pvm_upkbyte( char *xp, int nitem, int stride) int info = pvm_upkcplx( float *cp, int nitem, int stride) int info = pvm_upkdcplx( double *zp, int nitem, int stride) int info = pvm_upkdouble( double *dp, int nitem, int stride) int info = pvm_upkfloat( float *fp, int nitem, int stride) int info = pvm_upkint( int *ip, int nitem, int stride) int info = pvm_upkuint( unsigned int *ip, int nitem, int stride ) int info = pvm_upkushort( unsigned short *ip, int nitem, int stride ) int info = pvm_upkulong( unsigned long *ip, int nitem, int stride ) int info = pvm_upklong( long *ip, int nitem, int stride) int info = pvm_upkshort( short *jp, int nitem, int stride) int info = pvm_upkstr( char *sp ) Fortran call pvmfunpack( what, xp, nitem, stride, info ) PARAMETERS
fmt Printf-like format expression specifying what to pack. (See discussion) nitem The total number of items to be unpacked (not the number of bytes). stride The stride to be used when packing the items. For example, if stride = 2 in pvm_upkcplx, then every other complex number will be unpacked. xp Pointer to the beginning of a block of bytes. Can be any data type, but must match the corresponding pack data type. cp Complex array at least nitem*stride items long. zp Double precision complex array at least nitem*stride items long. dp Double precision real array at least nitem*stride items long. fp Real array at least nitem*stride items long. ip Integer array at least nitem*stride items long. jp Integer*2 array at least nitem*stride items long. sp Pointer to a null terminated character string. what Integer specifying the type of data being unpacked. what options STRING 0 REAL4 4 BYTE1 1 COMPLEX8 5 INTEGER2 2 REAL8 6 INTEGER4 3 COMPLEX16 7 info Integer status code returned by the routine. Values less than zero indicate an error. DESCRIPTION
Each of the pvm_upk* routines unpacks an array of the given data type from the active receive buffer. The arguments for each of the rou- tines are a pointer to the array to be unpacked into, nitem which is the total number of items to unpack, and stride which is the stride to use when unpacking. An exception is pvm_upkstr() which by definition unpacks a NULL terminated character string and thus does not need nitem or stride argu- ments. The Fortran routine pvmfunpack( STRING, ... ) expects nitem to be the number of characters in the string and stride to be 1. If the unpacking is successful, info will be 0. If some error occurs then info will be < 0. A single variable (not an array) can be unpacked by setting nitem = 1 and stride = 1. The routine pvm_unpackf() uses a printf-like format expression to specify what and how to unpack data from the receive buffer. All vari- ables are passed as addresses. A BNF-like description of the format syntax is: format : null | init | format fmt init : null | '%' '+' fmt : '%' count stride modifiers fchar fchar : 'c' | 'd' | 'f' | 'x' | 's' count : null | [0-9]+ | '*' stride : null | '.' ( [0-9]+ | '*' ) modifiers : null | modifiers mchar mchar : 'h' | 'l' | 'u' Formats: + means initsend - must match an int (how) in the param list. c pack/unpack bytes d integer f float x complex float s string Modifiers: h short (int) l long (int, float, complex float) u unsigned (int) Future extensions to the what argument in pvmfunpack will include 64 bit types when XDR encoding of these types is available. Meanwhile users should be aware that precision can be lost when passing data from a 64 bit machine like a Cray to a 32 bit machine like a SPARCsta- tion. As a mnemonic the what argument name includes the number of bytes of precision to expect. By setting encoding to PVMRAW (see pvmfinitsend) data can be transferred between two 64 bit machines with full precision even if the PVM configuration is heterogeneous. Messages should be unpacked exactly like they were packed to insure data integrity. Packing integers and unpacking them as floats will often fail because a type encoding will have occurred transferring the data between heterogeneous hosts. Packing 10 integers and 100 floats then trying to unpack only 3 integers and the 100 floats will also fail. EXAMPLES
C: info = pvm_recv( tid, msgtag ); info = pvm_upkstr( string ); info = pvm_upkint( &size, 1, 1 ); info = pvm_upkint( array, size, 1 ); info = pvm_upkdouble( matrix, size*size, 1 ); int count, *iarry; double darry[4]; pvm_unpackf("%d", &count); pvm_unpackf("%*d %4lf", count, iarry, darry); Fortran: CALL PVMFRECV( TID, MSGTAG, INFO ); CALL PVMFUNPACK( INTEGER4, NSIZE, 1, 1, INFO ) CALL PVMFUNPACK( STRING, STEPNAME, 8, 1, INFO ) CALL PVMFUNPACK( REAL4, A(5,1), NSIZE, NSIZE , INFO ) ERRORS
PvmNoData Reading beyond the end of the receive buffer. Most likely cause is trying to unpack more items than were originally packed into the buffer. PvmBadMsg The received message can not be decoded. Most likely because the hosts are heterogeneous and the user specified an incompatible encoding. Try setting the encoding to PvmDataDefault (see pvm_mkbuf). PvmNoBuf There is no active receive buffer to unpack. SEE ALSO
pvm_pack(3PVM) pvm_send(3PVM), pvm_recv(3PVM), pvm_pkmesg(3PVM) 30 August, 1993 UNPACK(3PVM)
Man Page