| 
		   | 
		
| Line 39: | 
Line 39: | 
|   | === Example ===  |   | === Example ===  | 
|   |  |   |  | 
| − | Convert a string into a binary array and back again:
  | + | Go see [[Core Function Pack|Pack( <expression>, <expressions> )]] for a list of formats and examples.  | 
| − |    | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | $arr = Pack("z0", "Hello World!")
  | + |  | 
| − | For $i In $arr
  | + |  | 
| − | 	println($i)
  | + |  | 
| − | Next
  | + |  | 
| − | $str = Unpack("z0", $arr)
  | + |  | 
| − | println($str)
  | + |  | 
| − | </syntaxhighlight>
  | + |  | 
| − |    | + |  | 
| − | Convert a dobule into a binary array and back again:
  | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | $arr = Pack("d", 777.42)
  | + |  | 
| − | For $i In $arr
  | + |  | 
| − | 	println($i)
  | + |  | 
| − | Next
  | + |  | 
| − | $str = Unpack("d", $arr)
  | + |  | 
| − | println($str)
  | + |  | 
| − | </syntaxhighlight>  | + |  | 
| − |    | + |  | 
| − | Convert a int into a binary array and back again:
  | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | $arr = Pack("d", (int)777)
  | + |  | 
| − | For $i In $arr
  | + |  | 
| − | 	println($i)
  | + |  | 
| − | Next
  | + |  | 
| − | $str = Unpack("d", $arr)
  | + |  | 
| − | println($str)
  | + |  | 
| − | </syntaxhighlight>  | + |  | 
| − |    | + |  | 
| − | Convert a string into a hex and back again:
  | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | $hex = Join(Unpack("*H", Pack("z0", "Hello World!")), '')
  | + |  | 
| − | println("Hex String: " . $hex)
  | + |  | 
| − | $str = Unpack("z0", Pack("h", $hex))
  | + |  | 
| − | println("Normal String: " . $str)
  | + |  | 
| − | </syntaxhighlight>
  | + |  | 
| − |    | + |  | 
| − | To pack two integers in big endian format, you would use this:
  | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | $bytes = Pack ("^ii", 1234, 4542);
  | + |  | 
| − | </syntaxhighlight>
  | + |  | 
| − |    | + |  | 
| − | More Examples:
  | + |  | 
| − | <syntaxhighlight lang="sputnik">
  | + |  | 
| − | ; 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
  | + |  | 
| − | </syntaxhighlight>
  | + |  | 
|   |  |   |  | 
|   | [[Category:Core Function]]  |   | [[Category:Core Function]]  | 
The format string to use.
An binary array of bytes such as one produced by the Pack function.
A string to be converted into a binary array of bytes (z0 ASCII encoding).
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.