Core Function Unpack

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> Unpack( <expression>, <binary-array> ) </pre> === Description === Pack data into a binary array === expression === The format string to use. ==== binary-array ==== An...")
 
(Remarks)
Line 21: Line 21:
  
 
=== Remarks ===
 
=== Remarks ===
 +
 +
Its worth noting you can cycle through a Binary variable like this :
 +
<syntaxhighlight lang="sputnik">
 +
$binary = Pack("z0", "Hello World!")
 +
For $i In $binary
 +
println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) )
 +
Next
 +
</syntaxhighlight>
  
 
==== Control Specifiers ====
 
==== Control Specifiers ====
Line 151: Line 159:
 
|null byte
 
|null byte
 
|}
 
|}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  
 
=== Example ===
 
=== Example ===

Revision as of 23:40, 11 November 2011

Unpack( <expression>, <binary-array> )

Contents

Description

Pack data into a binary array

expression

The format string to use.

binary-array

An array of bytes such as one provided by the Pack function.

Return Value

Success: Returns the data requested as a int, double, string or an array of data. Failure: Returns empty array.

Remarks

Its worth noting you can cycle through a Binary variable like this :

$binary = Pack("z0", "Hello World!")
For $i In $binary
	println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) )
Next

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

Character Description
s Int16
S UInt16
i Int32
I UInt32
l Int64
L UInt64
f float
d double
b byte
c 1-byte signed character
C 1-byte unsigned character
z8 string encoded as UTF8 with 1-byte null terminator
z6 string encoded as UTF16 with 2-byte null terminator
z7 string encoded as UTF7 with 1-byte null terminator
zb string encoded as BigEndianUnicode with 2-byte null terminator
z3 string encoded as UTF32 with 4-byte null terminator
z4 string encoded as UTF32 big endian with 4-byte null terminator
z0 ASCII string without a null terminator
z1 ASCII string with 1-byte terminator
$8 string encoded as UTF8
$6 string encoded as UTF16
$7 string encoded as UTF7
$b string encoded as BigEndianUnicode
$3 string encoded as UTF32
$4 string encoded as UTF-32 big endian encoding
x null byte

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
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox