"Sputnik" help  
Sputnik Help
Bin([<values...>])

Description

The Bin function is used to create a binary string. It accepts 0 or more values as arguments, which can be of various types. Here's an explanation of how the function works internally:

Overall, the Bin function provides a convenient way to construct binary strings by accepting various input types and converting them to binary representations. It supports individual values, arrays, and ranges, making it flexible for creating binary data structures.

Parameters

<values...> Any number of values to be converted to binary and combined to create the final binary string.

Return Value

Success: Returns a new binary string containing the values (or empty if no value(s) given).
Failure: Returns an empty binary string (this may be desired though).

Remarks

The Bin function provides a convenient way to construct binary strings by converting values to their binary representation. It supports a variety of input types, including strings, arrays, and ranges.

When using string values, Bin automatically converts them to UTF-8 binary representation. This ensures proper handling of Unicode characters and compatibility with binary operations.

For array inputs, each element is individually converted to its binary representation, and the resulting binary values are appended to the binary string.

Ranges can be specified using the rr operator. This allows you to generate a sequence of binary values based on a given range, such as hexadecimal or character ranges.

The Bin function simplifies the process of working with binary data by seamlessly converting different value types into their binary representations. It provides flexibility and convenience for manipulating binary strings and performing operations on binary data.

Note that the resulting binary string will consist of a sequence of bytes representing the binary values derived from the provided values.

Unlike Pack, Bin doesn't provide the flexibility to specify byte orders or complex format codes. It is primarily designed to convert values into binary representation using the machine's byte order. It simplifies the process of creating binary strings by automatically handling the conversion of values to their binary representation without the need for specifying format codes.

If you require more advanced packing capabilities, such as specifying byte orders, complex format codes, or handling endianness, then using Pack with the appropriate format codes would be the recommended approach in Sputnik. Pack provides a wide range of format codes that allow for more precise control over the binary representation of values.

Therefore, if you need fine-grained control over the binary packing process, Pack would be the appropriate function to use in Sputnik.

Related

Pack

Example

Example of usage:

my $bin = Bin("Hello", "Cat", 777, 73.4f, 'T', 'A' rr 'F', 0x10 rr 0x20);
say BinaryExpand($bin);

// PRINTS
// 00 | 48 65 6C 6C 6F 43 61 74 09 49 54 41 42 43 44 45 HelloCat.ITABCDE
// 01 | 46 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E F...............
// 02 | 1F 20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- .

The example demonstrates the usage of the Bin function to create a binary string from various input values. In this case, the input values include strings, numbers, characters, and ranges.

The Bin function takes each provided value and converts it into its binary representation. For strings, it converts the UTF-8 encoding of the characters into binary bytes. The strings "Hello" and "Cat" are converted to their respective UTF-8 binary representations.

Next, numeric values such as 777 and 73.4f are converted to their binary representations based on their respective data types. The integer value 777 is converted to its 32-bit binary representation, while the floating-point value 73.4f is converted to its 32-bit floating-point binary representation.

Characters, represented by single quotes, are converted to their binary representations. The characters 'T' and 'A' are converted to their corresponding ASCII binary values.

The rr operator is used to specify ranges. In this example, the range 'A' rr 'F' represents the hexadecimal range from 'A' to 'F'. The range 0x10 rr 0x20 represents the hexadecimal range from 0x10 to 0x20. These ranges are converted to their respective binary representations.

The resulting binary string is displayed using the BinaryExpand function, which presents the binary data in a formatted manner. Each line represents a chunk of 16 bytes, with the first column indicating the line number and the second column showing a vertical delimiter. The subsequent columns display the binary representation of the corresponding bytes.

Same as above but using arrays since any data type can be used here:

my $a = array("Hello", "Cat", 777, 73.4f, 'T', 'A' rr 'F', 0x10 rr 0x20);
my $bin = Bin("Testy", $a, 5);
say BinaryExpand($bin);
// PRINTS
// 00 | 54 65 73 74 79 00 00 09 49 54 41 42 43 44 45 46 Testy...ITABCDEF
// 01 | 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F ................
// 02 | 20 05 -- -- -- -- -- -- -- -- -- -- -- -- -- --  .

Overall, the Bin function simplifies the process of creating binary strings by automatically converting different input values to their binary representations, allowing for easy manipulation and analysis of binary data.


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