Core Function Pack
(→Data Encoding) |
|||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Pack( < | + | Pack( <format>, <args> ) |
</pre> | </pre> | ||
=== Description === | === Description === | ||
− | Pack data into a binary array | + | Pack data into a binary array. |
− | + | Pack given arguments into a binary string according to format. | |
− | + | === Parameters === | |
− | ==== | + | ==== format ==== |
− | + | The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data. | |
− | + | For a, A, b, B, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string. | |
− | + | Currently implemented formats are: | |
− | + | <pre> | |
+ | Code Description | ||
+ | a NUL-padded string | ||
+ | A SPACE-padded string | ||
+ | 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 | ||
+ | w signed UNICODE char | ||
+ | W unsigned UNICODE char | ||
+ | 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) | ||
+ | x NUL byte | ||
+ | X Back up one byte | ||
+ | @ NUL-fill to absolute position | ||
+ | </pre> | ||
− | === | + | ==== args ==== |
− | + | One or more variables to be used in the packing. | |
− | + | === Return Value === | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Success: Returns the new binary variable. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Failure: Returns null. | |
− | + | === Remarks === | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Note that Sputnik stores variables as signed/unsigned as needed which can be confusing if you are a PHP user using this Pack function basically when the format says Unsigned it is literal and the variable will be unsigned/signed as specified both the packing and unpacking. | |
− | + | This also means if the original value as an Int64 and you packed it with "i" then unpack it with "i" the new value will be an Int32 and not an Int64 since Sputnik sees no need to magically unpack everything to highest data type so "f" will actually give you a Float and not a Double and so on of course that doesn't mean you can't cast it to a double when getting from the return array. | |
− | + | 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 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | === Example === | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | <syntaxhighlight lang="sputnik"> | |
+ | $bin = pack("S", 65535); | ||
+ | $ray = unpack("S", $bin); | ||
+ | echo "UNSIGNED SHORT VAL = ", $ray[1], "\n"; | ||
− | === | + | $bin = pack("S", 65536); |
+ | $ray = unpack("S", $bin); | ||
+ | echo "OVERFLOW USHORT VAL = ", $ray[1], "\n"; | ||
+ | |||
+ | $bin = pack("V", 65536); | ||
+ | $ray = unpack("V", $bin); | ||
+ | echo "SAME AS ABOVE BUT WITH ULONG VAL = ", $ray[1], "\n"; | ||
+ | </syntaxhighlight> | ||
− | |||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $ | + | $binarydata = "\x04\x00\xa0\x00"; |
− | + | $array = Unpack("cchars/nint", $binarydata); | |
− | + | // The resulting array will contain the entries | |
− | + | // "chars" with value 4 and "int" with 160. | |
− | + | printr $array; | |
− | + | // Prints: | |
− | + | // ARRAY | |
− | + | // { | |
− | + | // [chars] => 4 | |
− | + | // [int] => 194 | |
+ | // } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $ | + | $binarydata = "\x04\x00\xa0\x00"; |
− | + | $array = Unpack("c2chars/nint", $binarydata); | |
− | + | // The resulting array will contain the entries | |
− | + | // "chars1", "chars2" and "int". | |
− | + | printr $array; | |
− | + | // Prints: | |
− | + | // ARRAY | |
− | + | // { | |
− | + | // [chars1] => 4 | |
− | + | // [chars2] => 0 | |
− | + | // [int] => 49824 | |
− | + | // } | |
− | + | ||
− | } | + | |
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $ | + | $binarydata = Pack("nvc*", 0x1234, 0x5678, 65, 66); |
− | // | + | // The resulting binary string will be 6 bytes long |
− | + | // and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42. | |
− | + | printr $binarydata; | |
− | { | + | // Prints: |
− | + | // {BINARY:6} | |
− | + | // { | |
− | + | // [0] => 18 | |
− | + | // [1] => 52 | |
− | + | // [2] => 120 | |
− | + | // [3] => 86 | |
− | + | // [4] => 65 | |
− | + | // [5] => 66 | |
− | } | + | // } |
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Using * to pack all the remaining objects with the same specifier | |
− | + | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | printr pack("C*",80,72,80); | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | String to Hex and back again | |
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | function H2Str( $hex ) | |
− | + | ||
{ | { | ||
− | + | return pack('H*', $hex); | |
} | } | ||
− | |||
− | |||
− | |||
− | + | function Str2H( $str ) | |
− | + | ||
− | + | ||
− | + | ||
{ | { | ||
− | + | return unpack('H*', $str, true); | |
} | } | ||
− | $ | + | $txt = 'This is test'; |
− | + | $hex = Str2H( $txt ); | |
+ | $str = H2Str( $hex ); | ||
+ | echo "${txt} => ${hex} => ${str}\n"; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Display the ASCII character codes for an entire string | |
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | echo join (unpack('C*', 'abcdef'), ' '); | |
− | + | // 97 98 99 100 101 102 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | + | Display the UNICODE character codes for an entire string | |
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | + | echo join (unpack('W*', 'こんにちは'), ' '); | |
− | + | // 33251 58259 37762 33251 58283 41345 33251 | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Revision as of 14:02, 26 September 2013
Pack( <format>, <args> )
Contents |
Description
Pack data into a binary array.
Pack given arguments into a binary string according to format.
Parameters
format
The format string consists of format codes followed by an optional repeater argument. The repeater argument can be either an integer value or * for repeating to the end of the input data.
For a, A, b, B, h, H the repeat count specifies how many characters of one data argument are taken, for @ it is the absolute position where to put the next data, for everything else the repeat count specifies how many data arguments are consumed and packed into the resulting binary string.
Currently implemented formats are:
Code Description a NUL-padded string A SPACE-padded string 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 w signed UNICODE char W unsigned UNICODE char 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) x NUL byte X Back up one byte @ NUL-fill to absolute position
args
One or more variables to be used in the packing.
Return Value
Success: Returns the new binary variable.
Failure: Returns null.
Remarks
Note that Sputnik stores variables as signed/unsigned as needed which can be confusing if you are a PHP user using this Pack function basically when the format says Unsigned it is literal and the variable will be unsigned/signed as specified both the packing and unpacking.
This also means if the original value as an Int64 and you packed it with "i" then unpack it with "i" the new value will be an Int32 and not an Int64 since Sputnik sees no need to magically unpack everything to highest data type so "f" will actually give you a Float and not a Double and so on of course that doesn't mean you can't cast it to a double when getting from the return array.
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
Example
$bin = pack("S", 65535); $ray = unpack("S", $bin); echo "UNSIGNED SHORT VAL = ", $ray[1], "\n"; $bin = pack("S", 65536); $ray = unpack("S", $bin); echo "OVERFLOW USHORT VAL = ", $ray[1], "\n"; $bin = pack("V", 65536); $ray = unpack("V", $bin); echo "SAME AS ABOVE BUT WITH ULONG VAL = ", $ray[1], "\n";
$binarydata = "\x04\x00\xa0\x00"; $array = Unpack("cchars/nint", $binarydata); // The resulting array will contain the entries // "chars" with value 4 and "int" with 160. printr $array; // Prints: // ARRAY // { // [chars] => 4 // [int] => 194 // }
$binarydata = "\x04\x00\xa0\x00"; $array = Unpack("c2chars/nint", $binarydata); // The resulting array will contain the entries // "chars1", "chars2" and "int". printr $array; // Prints: // ARRAY // { // [chars1] => 4 // [chars2] => 0 // [int] => 49824 // }
$binarydata = Pack("nvc*", 0x1234, 0x5678, 65, 66); // The resulting binary string will be 6 bytes long // and contain the byte sequence 0x12, 0x34, 0x78, 0x56, 0x41, 0x42. printr $binarydata; // Prints: // {BINARY:6} // { // [0] => 18 // [1] => 52 // [2] => 120 // [3] => 86 // [4] => 65 // [5] => 66 // }
Using * to pack all the remaining objects with the same specifier
printr pack("C*",80,72,80);
String to Hex and back again
function H2Str( $hex ) { return pack('H*', $hex); } function Str2H( $str ) { return unpack('H*', $str, true); } $txt = 'This is test'; $hex = Str2H( $txt ); $str = H2Str( $hex ); echo "${txt} => ${hex} => ${str}\n";
Display the ASCII character codes for an entire string
echo join (unpack('C*', 'abcdef'), ' '); // 97 98 99 100 101 102
Display the UNICODE character codes for an entire string
echo join (unpack('W*', 'こんにちは'), ' '); // 33251 58259 37762 33251 58283 41345 33251