Core Function PackSingle
From Sputnik Wiki
PackSingle( <format>, <value> )
Contents |
Description
Pack a single value into a binary array.
Parameters
format
Read Pack( ) to learn about format codes.
Some format codes may not exist in PackSingle() as they do in Pack().
For example you may only a repeater or * if the format code is either :
a, A, Z, h, H, z, b, B
Any other format code trying to use a reapter or * will result in error.
Currently implemented formats are:
Code Description a NUL-padded string (ASCII) A SPACE-padded string (ASCII) Z NUL-padded string (ASCII) a NUL-terminator will be added to the end regardless b A bit string (ascending bit order inside each byte, like the Vec() function) B A bit string (descending bit order inside each byte) h Hex string, low nibble first H Hex string, high nibble first c signed ASCII char C unsigned ASCII char U signed UNICODE char (always 16 bit, machine byte order) W unsigned UNICODE char (always 16 bit, machine byte order) s signed short (always 16 bit, machine byte order) S unsigned short (always 16 bit, machine byte order) n unsigned short (always 16 bit, big endian byte order) v unsigned short (always 16 bit, little endian byte order) i signed integer (machine dependent size and byte order) I unsigned integer (machine dependent size and byte order) l signed long (always 32 bit, machine byte order) L unsigned long (always 32 bit, machine byte order) q signed quad (64-bit) value (always 64 bit, machine byte order) Q unsigned quad (64-bit) value (always 64 bit, machine byte order) N unsigned long (always 32 bit, big endian byte order) V unsigned long (always 32 bit, little endian byte order) f float (machine dependent size and representation) d double (machine dependent size and representation) t A signed pointer T A unsigned pointer u A uuencoded string z7 string encoded as UTF8 with 1-byte null terminator z6 string encoded as UTF7 with 1-byte null terminator z5 string encoded as UTF16 with 2-byte null terminator z4 string encoded as BigEndianUnicode with 2-byte null terminator z3 string encoded as UTF32 big endian with 4-byte null terminator z2 string encoded as UTF32 with 4-byte null terminator z1 string encoded as ASCII with 1-byte null terminator z0 string encoded as ASCII without a null terminator
value
The value to be used in the packing.
Return Value
Success: Returns the new binary variable.
Failure: Returns null.
Remarks
This is almost the same as Pack( ) but designed to only do one operation thus avoiding the need for excessive parsing and what not the result is more speed.
Example
See Pack( ) for examples since almost everything there works here (just differance is how its done)
Simple example
// Pack 1337 into binary $a = PackSingle("i", 1337); // Pack 777 into binary $b = PackSingle("i", 777); // Combine them into a single binary $c = bin($a, $b); // We could use $a . $b // Read the first number say UnpackSingle("i", $c); // Read the first number (must use index) say UnpackSingle("i", $c, 4); // PRINTS // 1337 // 777