"Sputnik" help  
Sputnik Help
Binary(<value>)

Description

Casts a value as a binary representation. The function follows specific rules for different types: strings are converted to UTF-8 binary, integers/floats are converted to their respective machine order encoding byte sequences, and arrays have all their values recursively converted to binary using the same rules.

Parameters

<value> The value to be cast as a binary representation.

Return Value

Success: Returns the value to be cast as a binary string representation.
Failure: Returns an empty binary string.

Remarks

The Binary function allows you to convert different types of values into their corresponding binary string representations. Depending on the type of value, specific rules are followed during the conversion process.

For strings, the UTF-8 encoding is used to convert each character into a binary string sequence. This ensures proper handling of multibyte characters and maintains compatibility with Unicode.

For integers and floats, the binary representation is based on the machine's order encoding. The resulting binary strings reflect the internal representation of these numeric types.

For arrays, the Binary function recursively converts each value in the array to its binary string representation, following the same rules described above for strings, integers, and floats.

Related

Casting

Example

Example of usage:

say Binary("Hello World")[<>];
say Binary(700)[<>];
say Binary(700.0f)[<>];
say Binary(700.0)[<>];
say Binary(array(10, 10.0f, 20.0, "Hello!", array("cat", 100, 200)))[<>];

// PRINTS
// 48 65 6C 6C 6F 20 57 6F 72 6C 64 -- -- -- -- -- Hello World
// BC 02 00 00 00 00 00 00 -- -- -- -- -- -- -- -- ........
// 00 00 2F 44 -- -- -- -- -- -- -- -- -- -- -- -- ../D
// 00 00 00 00 00 E0 85 40 -- -- -- -- -- -- -- -- .....à.@
// 0A 00 00 00 00 00 00 00 00 00 20 41 00 00 00 00 .......... A....
// 00 00 34 40 48 65 6C 6C 6F 21 63 61 74 64 00 00 ..4@Hello!catd..
// 00 00 00 00 00 C8 00 00 00 00 00 00 00 -- -- -- .....È.......

Casting:

say ((Binary)"Hello World")[<>];
// PRINTS
// 48 65 6C 6C 6F 20 57 6F 72 6C 64 -- -- -- -- -- Hello World

Heres one example of unpacking the data again using a Struct:

my $bin = Binary(array(10, 10.0f, 20.0, "Hello!", array("cat", 100, 200)));

my $st = Struct(q~
    struct PACKED {
    int64_t vi;
    float_t vf;
    double_t vfd;
    char_t vt[6];
        struct PACKED {
        char_t vt[3];
        int64_t vi;
        int64_t vi2;
        } Nest;
    } TESTY;
~);

printr FromStruct($st, $bin);

// PRINTS
// Array
// (
//     [vi] => 10
//     [vf] => 10
//     [vfd] => 20
//     [vt] => Hello!
//     [Nest] => Array
//         (
//             [vt] => cat
//             [vi] => 100
//             [vi2] => 200
//         )
// )

Same as above but using a TypeDef:

my $bin = Binary(array(10, 10.0f, 20.0, "Hello!", array("cat", 100, 200)));

TypeDef(q~
    struct PACKED {
    int64_t vi;
    float_t vf;
    double_t vfd;
    char_t vt[6];
        struct PACKED {
        char_t vt[3];
        int64_t vi;
        int64_t vi2;
        } Nest;
    } TESTY;
~);

printr (TESTY)$bin;

// PRINTS
// Array
// (
//     [vi] => 10
//     [vf] => 10
//     [vfd] => 20
//     [vt] => Hello!
//     [Nest] => Array
//         (
//             [vt] => cat
//             [vi] => 100
//             [vi2] => 200
//         )
// )

 


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org