Core Function BinaryLen
From Sputnik Wiki
(Difference between revisions)
(→Example) |
m (1 revision) |
||
(3 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | BinaryLen( <binary-array> ) | + | BinaryLen( <binary-array>, <newLen>, <fillWith> ) |
</pre> | </pre> | ||
Line 6: | Line 6: | ||
Returns the number of bytes in a binary variable. | Returns the number of bytes in a binary variable. | ||
+ | |||
+ | Or increase it's capacity for bytes by using the second parameter. | ||
=== Parameters === | === Parameters === | ||
Line 11: | Line 13: | ||
==== binary-array ==== | ==== binary-array ==== | ||
− | The binary variable to | + | The binary variable to check its size. |
+ | |||
+ | ==== newLen ==== | ||
+ | |||
+ | Optional; If provided the Binary variable data array will be expanded to this new size. | ||
+ | |||
+ | Note - new lengths below the size of the current length are totally ignored and do nothing. | ||
+ | |||
+ | ==== fillWith ==== | ||
+ | |||
+ | Optional; The byte to fill in new bytes with if using the newLen | ||
+ | |||
+ | Default: 0x00 | ||
=== Return Value === | === Return Value === | ||
+ | |||
+ | ==== If using only one parameter ==== | ||
Success: Returns the length of the binary data in bytes. | Success: Returns the length of the binary data in bytes. | ||
+ | |||
+ | Failure: Returns -1. | ||
+ | |||
+ | ==== If using the newLen parameter ==== | ||
+ | |||
+ | Success: Returns true. | ||
Failure: Returns -1. | Failure: Returns -1. | ||
Line 21: | Line 43: | ||
=== Remarks === | === Remarks === | ||
− | UBound works just as good for this. | + | UBound works just as good for this if you only wish to see its current size. |
=== Example === | === Example === | ||
Line 33: | Line 55: | ||
println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | ||
} | } | ||
− | $String = Unpack(" | + | $String = Unpack("A*", $binary, 3); |
println("Full string: " . $String); | println("Full string: " . $String); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of setting the length | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Make a binary of "Hello" | ||
+ | my $a = Pack("A*", "Hello"); | ||
+ | // Print it | ||
+ | say BinaryExpand($a); | ||
+ | // Expand its size | ||
+ | BinaryLen($a, 11); | ||
+ | // Write more data | ||
+ | $a[5] = @' '; | ||
+ | $a[6] = @'w'; | ||
+ | $a[7] = @'o'; | ||
+ | $a[8] = @'r'; | ||
+ | $a[9] = @'l'; | ||
+ | $a[10] = @'d'; | ||
+ | // Print it | ||
+ | say BinaryExpand($a); | ||
+ | // Prints: | ||
+ | // 48 65 6C 6C 6F -- -- -- -- -- -- -- -- -- -- -- Hello | ||
+ | // 48 65 6C 6C 6F 20 77 6F 72 6C 64 -- -- -- -- -- Hello world | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of setting the length and fill with | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | // Make a binary of "Hello" | ||
+ | my $a = Pack("A*", "Hello"); | ||
+ | // Print it | ||
+ | say BinaryExpand($a); | ||
+ | // Expand its size | ||
+ | BinaryLen($a, 100, 0x4A); | ||
+ | // Print it | ||
+ | say BinaryExpand($a); | ||
+ | // Prints: | ||
+ | // 48 65 6C 6C 6F -- -- -- -- -- -- -- -- -- -- -- Hello | ||
+ | // 48 65 6C 6C 6F 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A HelloJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ | ||
+ | // 4A 4A 4A 4A -- -- -- -- -- -- -- -- -- -- -- -- JJJJ | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:38, 14 June 2015
BinaryLen( <binary-array>, <newLen>, <fillWith> )
Contents |
Description
Returns the number of bytes in a binary variable.
Or increase it's capacity for bytes by using the second parameter.
Parameters
binary-array
The binary variable to check its size.
newLen
Optional; If provided the Binary variable data array will be expanded to this new size.
Note - new lengths below the size of the current length are totally ignored and do nothing.
fillWith
Optional; The byte to fill in new bytes with if using the newLen
Default: 0x00
Return Value
If using only one parameter
Success: Returns the length of the binary data in bytes.
Failure: Returns -1.
If using the newLen parameter
Success: Returns true.
Failure: Returns -1.
Remarks
UBound works just as good for this if you only wish to see its current size.
Example
$binary = BinaryHex("48656c6c6f20576f726c6421"); println( "Binary HEX: '" . BinaryStr($binary, ",") . "'" ); println("The binary size is: " . BinaryLen($binary) ); Foreach ($binary as $i) { println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); } $String = Unpack("A*", $binary, 3); println("Full string: " . $String);
Example of setting the length
// Make a binary of "Hello" my $a = Pack("A*", "Hello"); // Print it say BinaryExpand($a); // Expand its size BinaryLen($a, 11); // Write more data $a[5] = @' '; $a[6] = @'w'; $a[7] = @'o'; $a[8] = @'r'; $a[9] = @'l'; $a[10] = @'d'; // Print it say BinaryExpand($a); // Prints: // 48 65 6C 6C 6F -- -- -- -- -- -- -- -- -- -- -- Hello // 48 65 6C 6C 6F 20 77 6F 72 6C 64 -- -- -- -- -- Hello world
Example of setting the length and fill with
// Make a binary of "Hello" my $a = Pack("A*", "Hello"); // Print it say BinaryExpand($a); // Expand its size BinaryLen($a, 100, 0x4A); // Print it say BinaryExpand($a); // Prints: // 48 65 6C 6C 6F -- -- -- -- -- -- -- -- -- -- -- Hello // 48 65 6C 6C 6F 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A HelloJJJJJJJJJJJ // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ // 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A 4A JJJJJJJJJJJJJJJJ // 4A 4A 4A 4A -- -- -- -- -- -- -- -- -- -- -- -- JJJJ