Core Function Pack
From Sputnik Wiki
				
				
				(Difference between revisions)
				
																
				
				
								
				 (→Example)  | 
		 (→Remarks)  | 
		||
| Line 22: | Line 22: | ||
=== Remarks ===  | === Remarks ===  | ||
| − | + | ==== Control Specifiers ====  | |
| + | |||
| + | {| border="1" align="left" style="text-align:center;"  | ||
| + | |Character  | ||
| + | |Description  | ||
| + | |-  | ||
| + | |^  | ||
| + | |Switch to big endian encoding  | ||
| + | |-  | ||
| + | |_  | ||
| + | |Switch to little endian encoding  | ||
| + | |-  | ||
| + | |%  | ||
| + | |Switch to host (native) encoding  | ||
| + | |-  | ||
| + | |!  | ||
| + | |Aligns the next data type to its natural boundary, for example for a double that would be 8 bytes, for a 32-bit int, that would be 4 bytes. For strings this is set to 4.  | ||
| + | |-  | ||
| + | |N  | ||
| + | |A number between 1 and 9, indicates a repeat count (process N items with the following datatype  | ||
| + | |-  | ||
| + | |[N]  | ||
| + | |For numbers larger than 9, use brackets, for example [20]  | ||
| + | |-  | ||
| + | |*  | ||
| + | |Repeat the next data type until the arguments are exhausted  | ||
| + | |}  | ||
| + | |||
| + | ==== Data Encoding ====  | ||
=== Example ===  | === Example ===  | ||
Revision as of 22:53, 11 November 2011
Pack( <expression>, <expressions> )
Contents | 
Description
Pack data into a binary array
expression
The format string to use
variable
The variable to check.
Return Value
Success: Returns array filled with data. Failure: Returns empty array.
Remarks
Control Specifiers
| Character | Description | 
| ^ | Switch to big endian encoding | 
| _ | Switch to little endian encoding | 
| % | Switch to host (native) encoding | 
| ! | Aligns the next data type to its natural boundary, for example for a double that would be 8 bytes, for a 32-bit int, that would be 4 bytes. For strings this is set to 4. | 
| N | A number between 1 and 9, indicates a repeat count (process N items with the following datatype | 
| [N] | For numbers larger than 9, use brackets, for example [20] | 
| * | Repeat the next data type until the arguments are exhausted | 
Data Encoding
Example
Convert a string into a binary array and back again:
$arr = Pack("z0", "Hello World!") For $i In $arr println($i) Next $str = Unpack("z0", $arr) println($str)
Convert a dobule into a binary array and back again:
$arr = Pack("d", 777.42) For $i In $arr println($i) Next $str = Unpack("d", $arr) println($str)
Convert a int into a binary array and back again:
$arr = Pack("d", (int)777) For $i In $arr println($i) Next $str = Unpack("d", $arr) println($str)
Convert a string into a hex and back again:
$hex = Join(Unpack("*H", Pack("z0", "Hello World!")), '') println("Hex String: " . $hex) $str = Unpack("z0", Pack("h", $hex)) println("Normal String: " . $str)
To pack two integers in big endian format, you would use this:
$bytes = Pack ("^ii", 1234, 4542);
More Examples:
; The following means: ; Little endian encoding of a Int16, followed by an aligned ; int32 value. $r = Pack("_s!i", 0x7b, 0x12345678); For $i In $r print(Hex($i) . " ") Next ; Prints 7B 0 0 0 80 56 34 12 $bytes = Pack("CCCC", 65, 66, 67, 68) println($bytes) ; Prints 4-byte sequence for "ABCD" $bytes = Pack("4C", 65, 66, 67, 68) println($bytes) ; Prints 4-byte sequence for "ABCD" $bytes = Pack("*C", 65, 66, 67, 68) println($bytes) ; Prints 4-byte sequence for "ABCD" $bytes = Pack("^ii", 0x1234abcd, 0x7fadb007) For $i In $bytes print(Hex($i) . " ") Next println("") ; Result: 12 34 ab cd 7f ad b0 07 ; Encode 3 integers as big-endian, but only provides two as arguments, ; this defaults to zero for the last value. $bytes = Pack("^iii", 0x1234abcd, 0x7fadb007) For $i In $bytes print(Hex($i) . " ") Next println("") ; Result: 12 34 ab cd 7f ad b0 07 00 00 00 00 ; Encode as little endian, pack 1 short, align, 1 int $bytes = Pack("_s!i", 0x7b, 0x12345678); For $i In $bytes print(Hex($i) . " ") Next println("") ; Result: 7b 00 00 00 78 56 34 12 ; Encode a string in utf-8 with a null terminator $bytes = Pack("z8", "hello") For $i In $bytes print(Hex($i) . " ") Next println("") ; Result: 68 65 6c 6c 6f 00 00 00 00 ; Little endian encoding, for Int16, followed by an aligned ; Int32 $bytes = Pack("_s!i", 0x7b, 0x12345678) For $i In $bytes print(Hex($i) . " ") Next println("") ; Result: 7b 00 00 00 78 56 34 12