Core Function BinaryHex
From Sputnik Wiki
(Difference between revisions)
(→Example) |
m (1 revision) |
||
(7 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | BinaryHex( <expression> ) | + | BinaryHex( <expression>, <flag> ) |
</pre> | </pre> | ||
Line 12: | Line 12: | ||
A string of hex to convert into a binary variable. | A string of hex to convert into a binary variable. | ||
+ | |||
+ | ==== flag ==== | ||
+ | |||
+ | Optional; There are a few flags that can be turned on or off using the | operator | ||
+ | |||
+ | 1 = The string actually is hex using \x escape and is to be treated as such | ||
+ | |||
+ | 2 = Add a zero to the end of the binary data (null termination) | ||
+ | |||
+ | (To make that flag has null termination and uses a \x string you would do 1 | 2) | ||
=== Return Value === | === Return Value === | ||
Line 17: | Line 27: | ||
Success: Returns the binary variable. | Success: Returns the binary variable. | ||
− | Failure: Returns | + | Failure: Returns null. |
=== Remarks === | === Remarks === | ||
Line 26: | Line 36: | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
− | $binary = BinaryHex("48656c6c6f20576f726c6421") | + | $binary = BinaryHex("48656c6c6f20576f726c6421"); |
− | println( "Binary HEX: '" . BinaryStr($binary, ",") . "'" ) | + | println( "Binary HEX: '" . BinaryStr($binary, ",") . "'" ); |
− | println("The binary size is: " . | + | println("The binary size is: " . BinaryLen($binary) ); |
− | + | Foreach ($binary as $i) | |
− | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ) | + | { |
− | + | println( "Byte: " . $i . " | Hex: " . Hex($i) . " | Char: " . Chr($i) ); | |
− | $String = Unpack(" | + | } |
− | println("Full string: " . $String) | + | $String = Unpack("A*", $binary, 3); |
+ | println("Full string: " . $String); | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of using the flag | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | $binary = BinaryHex("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21", true); | ||
+ | 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); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Core Function]] | [[Category:Core Function]] |
Latest revision as of 12:38, 14 June 2015
BinaryHex( <expression>, <flag> )
Contents |
Description
Create a binary array from a hex string.
Parameters
expression
A string of hex to convert into a binary variable.
flag
Optional; There are a few flags that can be turned on or off using the | operator
1 = The string actually is hex using \x escape and is to be treated as such
2 = Add a zero to the end of the binary data (null termination)
(To make that flag has null termination and uses a \x string you would do 1 | 2)
Return Value
Success: Returns the binary variable.
Failure: Returns null.
Remarks
The string must contain only the hex chars nothing else not even 0x or spaces.
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 using the flag
$binary = BinaryHex("\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21", true); 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);