"Sputnik" help  
Sputnik Help
BinaryExpand(<binary>, [<usePrefix>],[<start>],[<length>])

Description

This function takes a binary string as input and expands it into a more readable format, resembling a hex editor. The expanded string provides an easier way to visualize and analyze the binary content.

Parameters

<binary> The binary string to be expanded.
<usePrefix> Optional boolean value indicating whether to include the line numbers prefix. (Default: true)
<start> Optional starting position in the binary string for expansion. (Default: 0)
<length> Optional length of the portion to be expanded. (Default: Entire string)

Return Value

Success: Returns the expanded binary string in a more readable format similar a hex-editor.
Failure: Returns empty string.

Remarks

The BinaryExpand, BinaryExpandPrint, and BinaryExpandPrintLn functions are designed to provide a comprehensive visual representation of binary data. The expanded format resembles a hex editor, displaying line numbers, hex codes (16 per line), and an accompanying snippet of ASCII characters. In situations where a binary value does not correspond to a printable character, the '.' symbol is used to indicate that the character cannot be displayed. This visual representation allows for easy analysis and interpretation of binary data, aiding in debugging, data exploration, and other related tasks.

Related

BinaryExpandPrint, BinaryExpandPrintLn

Example

Show the content of a binary string:

my $bin = (Binary)"Hello World";
say BinaryExpand($bin, false);
// PRINTS
// 48 65 6C 6C 6F 20 57 6F 72 6C 64 -- -- -- -- -- Hello World

Before this function was made internal it was part of the library and made in Sputnik here is a demonstration of it:

Function print_r($data)
{
    my $length = length($data);
    for(my $i = 0; $i < $length; $i++)
    {
        if ($i > 0 && $i % 16 == 0)
            printf("\n");
        printf("%02X ", $data[$i]);
        if (($i + 1) % 16 == 0 || $i + 1 == $length)
        {
            while (($i + 1) % 16 != 0)
            {
                printf("   ");
                $i++;
            }
            printf(" ");
            for(my $j = $i - 15; $j <= $i; $j++)
            {
                if ($j < $length)
                {
                    if(OfPrintable((Char)$data[$j]))
                        printf("%c", (Char)$data[$j]);
                    else
                        printf(".");
                }
            }
        }
    }
    printf("\n");
}

my $type = Struct(q~
    struct {
        struct {
            unsigned short english : 1;
            unsigned short paperBack : 1;
            unsigned short old : 1;
        } flags;
        char_t title[50];
        char_t author[50];
        char_t subject[100];
        uint32_t bookId;
    } Books;
~);

my $book = StructInst($type, 1, 0, 1, "Sputnik Programming", "Moo Moo", "Telecom Programming Tutorial", UniqueId() % @UInt32Max);

print_r($book);

// PRINTS
// 05 00 53 70 75 74 6E 69 6B 20 50 72 6F 67 72 61  ..Sputnik Progra
// 6D 6D 69 6E 67 00 00 00 00 00 00 00 00 00 00 00  mming...........
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 4D 6F 6F 20 4D 6F 6F 00 00 00 00 00  ....Moo Moo.....
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 00 00 54 65 6C 65 63 6F 6D 20 50 72  ......Telecom Pr
// 6F 67 72 61 6D 6D 69 6E 67 20 54 75 74 6F 72 69  ogramming Tutori
// 61 6C 00 00 00 00 00 00 00 00 00 00 00 00 00 00  al..............
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
// 00 00 00 00 00 00 00 00 00 00 00 00 E6 27 BC 75  .............'.u

 


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