Core Function BinaryToStr

From Sputnik Wiki
(Difference between revisions)
Jump to: navigation, search
(Created page with "<pre> BinaryToStr( <binary-array> ) </pre> === Description === Returns a new string containing the binary data as raw bytes. === Parameters === ==== binary-array ==== The bi...")
 
m (1 revision)
 
(10 intermediate revisions by one user not shown)
Line 1: Line 1:
 
<pre>
 
<pre>
BinaryToStr( <binary-array> )
+
BinaryToStr( <binary-array>, <flag>, <start>, <length> )
 
</pre>
 
</pre>
  
Line 12: Line 12:
  
 
The binary variable to use.  
 
The binary variable to use.  
 +
 +
==== flag ====
 +
 +
Optional; Encoding to use.
 +
 +
Choices are:
 +
 +
"RAW" = Forces each byte in the binary to directly become a character in the string
 +
 +
"UNICODE" = Alias to UTF8 (or whatever Sputnik is using for Unicode strings)
 +
 +
"ASCII"
 +
 +
"UTF8"
 +
 +
"UTF7"
 +
 +
"UTF32"
 +
 +
Default: "RAW"
  
 
=== Return Value ===
 
=== Return Value ===
Line 19: Line 39:
 
=== Remarks ===
 
=== Remarks ===
  
None.
+
Sputnik strings allow null terminators inside them so they can be used just like raw binary data this is useful since you can treat it as a string and use all the string functions but yet it's also binary.
  
 
=== Example ===
 
=== Example ===
 +
 +
ASCII Example
  
 
<syntaxhighlight lang="sputnik">
 
<syntaxhighlight lang="sputnik">
$binary = Pack("z0iii", "Hello World!", 100, 200, 300);
+
$binary = Pack("A*", "Hello World!");
echo "First binary variable printed BELOW\n";
+
printr($binary);
+
echo "First binary variable printed ABOVE\n";
+
 
$binStr = BinaryToStr($binary);
 
$binStr = BinaryToStr($binary);
 
echo "String Content: $binStr\n";
 
echo "String Content: $binStr\n";
echo "Second binary variable printed BELOW\n";
+
// Prints:
printr(BinaryFromStr($binStr));
+
// String Content: Hello World!
echo "Second binary variable printed ABOVE\n";
+
</syntaxhighlight>
/*
+
 
Prints:
+
UNICODE (UTF8) Example
First binary variable printed BELOW
+
 
{BINARY:24}
+
<syntaxhighlight lang="sputnik">
{
+
$binary = BinaryFromStr("ふふふ", "UTF8");
        [0] => 72
+
printr $binary;
        [1] => 101
+
$binStr = BinaryToStr($binary, "UTF8");
        [2] => 108
+
echo "String Content: $binStr\n";
        [3] => 108
+
// Prints:
        [4] => 111
+
// String Content: ふふふ
        [5] => 32
+
</syntaxhighlight>
        [6] => 87
+
 
        [7] => 111
+
Using start parameter to get everything starting with World
        [8] => 114
+
 
        [9] => 108
+
<syntaxhighlight lang="sputnik">
        [10] => 100
+
$binary = BinaryFromStr("Hello World!", "ASCII");
        [11] => 33
+
$binStr = BinaryToStr($binary, "ASCII", 6);
        [12] => 100
+
echo "String Content: $binStr\n";
        [13] => 0
+
// Prints:
        [14] => 0
+
// String Content: World!
        [15] => 0
+
</syntaxhighlight>
        [16] => 200
+
 
        [17] => 0
+
Using start and length parameter to get just World
        [18] => 0
+
 
        [19] => 0
+
<syntaxhighlight lang="sputnik">
        [20] => 44
+
$binary = BinaryFromStr("Hello World!", "ASCII");
        [21] => 1
+
$binStr = BinaryToStr($binary, "ASCII", 6, 5);
        [22] => 0
+
echo "String Content: $binStr\n";
        [23] => 0
+
// Prints:
}
+
// String Content: World
First binary variable printed ABOVE
+
String Content: Hello World!d  È  ,☺
+
Second binary variable printed BELOW
+
{BINARY:24}
+
{
+
        [0] => 72
+
        [1] => 101
+
        [2] => 108
+
        [3] => 108
+
        [4] => 111
+
        [5] => 32
+
        [6] => 87
+
        [7] => 111
+
        [8] => 114
+
        [9] => 108
+
        [10] => 100
+
        [11] => 33
+
        [12] => 100
+
        [13] => 0
+
        [14] => 0
+
        [15] => 0
+
        [16] => 200
+
        [17] => 0
+
        [18] => 0
+
        [19] => 0
+
        [20] => 44
+
        [21] => 1
+
        [22] => 0
+
        [23] => 0
+
}
+
Second binary variable printed ABOVE
+
*/
+
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Core Function]]
 
[[Category:Core Function]]

Latest revision as of 12:37, 14 June 2015

BinaryToStr( <binary-array>, <flag>, <start>, <length> )

Contents

Description

Returns a new string containing the binary data as raw bytes.

Parameters

binary-array

The binary variable to use.

flag

Optional; Encoding to use.

Choices are:

"RAW" = Forces each byte in the binary to directly become a character in the string

"UNICODE" = Alias to UTF8 (or whatever Sputnik is using for Unicode strings)

"ASCII"

"UTF8"

"UTF7"

"UTF32"

Default: "RAW"

Return Value

Returns a new string containing the binary data as raw bytes.

Remarks

Sputnik strings allow null terminators inside them so they can be used just like raw binary data this is useful since you can treat it as a string and use all the string functions but yet it's also binary.

Example

ASCII Example

$binary = Pack("A*", "Hello World!");
$binStr = BinaryToStr($binary);
echo "String Content: $binStr\n";
// Prints:
// String Content: Hello World!

UNICODE (UTF8) Example

$binary = BinaryFromStr("ふふふ", "UTF8");
printr $binary;
$binStr = BinaryToStr($binary, "UTF8");
echo "String Content: $binStr\n";
// Prints:
// String Content: ふふふ

Using start parameter to get everything starting with World

$binary = BinaryFromStr("Hello World!", "ASCII");
$binStr = BinaryToStr($binary, "ASCII", 6);
echo "String Content: $binStr\n";
// Prints:
// String Content: World!

Using start and length parameter to get just World

$binary = BinaryFromStr("Hello World!", "ASCII");
$binStr = BinaryToStr($binary, "ASCII", 6, 5);
echo "String Content: $binStr\n";
// Prints:
// String Content: World
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox