"Sputnik" help  
Sputnik Help
BinaryPad(<binary>, <totalWidth>, [<paddingType>], [<paddingByte>])

Description

The BinaryPad function is used to pad a binary string to a specified total width with a specified padding type and padding byte.

Parameters

<binary> The original binary string to be padded.
<totalWidth> The desired total width of the resulting binary string.
<paddingType> Optional padding type to determine where the padding will be applied. It can take the following values:
@PaddingBoth: Pad on both the start and end of the binary string.
@PaddingStart: Pad only on the start (left side) of the binary string.
@PaddingEnd: Pad only on the end (right side) of the binary string. (Default if not specified)
<paddingByte> Optional byte used for padding. If not specified, the default padding byte is 0x20 (space).

Return Value

Success: Returns a new binary string with the specified total width, padded according to the padding type and padding byte.
Failure: Returns an empty binary string.

Remarks

If the <binary> string is already equal to or longer than the <totalWidth>, the function will return the original binary string unchanged.
The <paddingType> determines where the padding will be applied:
@PaddingBoth: The padding will be added on both the start and end of the binary string to achieve the desired <totalWidth>.
@PaddingStart: The padding will be added only on the start (left side) of the binary string.
@PaddingEnd: The padding will be added only on the end (right side) of the binary string. This is the default if the <paddingType> is not specified.
The <paddingByte> is used to fill the space during padding. It can be any byte value.
The resulting binary string will have a length equal to <totalWidth>. If the <binary> string is shorter than <totalWidth>, the remaining space will be filled with the specified <paddingByte>.
The original binary string is not modified; instead, a new binary string is created with the padding applied.

Related

BinaryPadLeft, BinaryPadRight

Example

Example of all usage types:

my $binary = BinaryFromStr("Hello");
my $paddedBoth = BinaryPad($binary, 10, @PaddingBoth, 0x20);
my $paddedStart = BinaryPad($binary, 10, @PaddingStart, 0x20);
my $paddedEnd = BinaryPad($binary, 10, @PaddingEnd, 0x20);
say "'$paddedBoth'";
say "'$paddedStart'";
say "'$paddedEnd'";

// PRINTS
// '          Hello          '
// '          Hello'
// 'Hello          '

 


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