Core Function Unpack

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Return Value)
Line 23: Line 23:
 
=== Return Value ===
 
=== Return Value ===
  
Success: Returns the data requested as a int, double, string or an array of data.
+
Success: Returns the array of data (Even if its only 1 item) unless the flag is set to 1 then it will return a string.
  
 
Failure: Returns empty array.
 
Failure: Returns empty array.

Revision as of 12:43, 17 November 2011

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

Contents

Description

Unpack data from a binary array

expression

The format string to use.

binary-array

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

flag

Optional; Control what is returned options are: 0 = Return an array 1 = Return a string

Return Value

Success: Returns the array of data (Even if its only 1 item) unless the flag is set to 1 then it will return a string.

Failure: Returns empty array.

Remarks

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

$binary = Pack("z0", "Hello World!")
; Yes you can get the size of the binary byte array by using UBound just like with arrays
println("The binary size is: " . UBound($binary) )
For $i In $binary
	println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) )
Next

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