Core Function BinaryClone

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
m (1 revision)
 
(5 intermediate revisions by one user not shown)
Line 15: Line 15:
 
=== Return Value ===
 
=== Return Value ===
  
Success: Returns 1.
+
Success: Returns cloned binary array.
  
Failure: Returns 0.
+
Failure: Returns null.
  
 
=== Remarks ===
 
=== Remarks ===
Line 31: Line 31:
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$binary = Pack("z0", "Hello World!");
+
$binary = Pack("A*", "Hello World!");
 
$binary2 = BinaryClone($binary);
 
$binary2 = BinaryClone($binary);
 
BinaryReverse($binary);
 
BinaryReverse($binary);
println("b1: " . Unpack("z0", $binary));
+
println("b1: " . Unpack("A*", $binary, 3));
println("b2: " . Unpack("z0", $binary2));
+
println("b2: " . Unpack("A*", $binary2, 3));
 +
</syntaxhighlight>
 +
 
 +
Alternative way using Clone()
 +
 
 +
<syntaxhighlight lang="sputnik">
 +
$a = BinaryCreate(3);
 +
$a[0] = 'C';
 +
$a[1] = 'A';
 +
$a[2] = 'T';
 +
 
 +
$b = Clone($a);
 +
$b[0] = 'J';
 +
 
 +
echo "First '$a' Second '$b'\n";
 +
# Prints
 +
# First 'CAT' Second 'JAT'
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:38, 14 June 2015

BinaryClone( <binary-array> )

Contents

Description

Clone a binary variable 100% and return a new binary variable with exactly same data as the old one.

Parameters

binary-array

The binary variable to use.

Return Value

Success: Returns cloned binary array.

Failure: Returns null.

Remarks

Contrary to what you might think simply doing

$binary2 = $binary1

That does not *copy* the data it will simply creates a reference to it making a change at either variable will change both so if you wish to actually copy a binary variables data you will need to use BinaryClone or BinaryMid etc.

Example

$binary = Pack("A*", "Hello World!");
$binary2 = BinaryClone($binary);
BinaryReverse($binary);
println("b1: " . Unpack("A*", $binary, 3));
println("b2: " . Unpack("A*", $binary2, 3));

Alternative way using Clone()

$a = BinaryCreate(3);
$a[0] = 'C';
$a[1] = 'A';
$a[2] = 'T';
 
$b = Clone($a);
$b[0] = 'J';
 
echo "First '$a' Second '$b'\n";
# Prints
# First 'CAT' Second 'JAT'
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox