"Sputnik" help  
Sputnik Help
Tb(<string>)

Description

Converts a text string (<string>) to a network byte order 32-bit integer. The Tb function is commonly used to convert text-based values, such as magic numbers in file headers or network protocols, to a standardized byte order for compatibility across different systems.

Parameters

<string> The text string to convert to a network byte order 32-bit integer.

Return Value

Success: Returns 32-bit integer representing the network byte order of the converted text string.
Failure: Returns an incomplete 32-bit integer representing the network byte order of the converted text string..

Remarks

The Tb function assumes that the input text string represents a valid network byte order 32-bit integer. It performs the necessary byte order conversion to ensure consistency across different systems. It is important to note that the function does not perform any validation on the input string, so ensure that the input is a valid representation of a network byte order 32-bit integer.

It is important to note that the function does not perform any validation on the input string. To ensure compatibility with 4-letter values, the input string should consist of exactly 4 characters. If the input string is shorter than 4 characters, you can use periods (.) to fill the remaining positions.

Sputnik aims to provide a comprehensive set of features and functions, including those that may be less common or niche, to cater to a wide range of programming needs. By offering the Tb function, Sputnik ensures compatibility with systems or protocols that still require the manipulation of network byte order values.

Related

Bt

Example

Example of usage:

// using as numeric
my $a = Tb16("P.");
my $b = Tb("PNG.");
my $c = Tb64("CSS-HTML");

say $a;
say $b;
say $c;

say Bt16($a);
say Bt($b);
say Bt64($c);

// using as binary
my $bin = Pack("sik", $a, $b, $c);
say BinaryExpand($bin, false);
my list ($aa, $bb, $cc) = Unpack("s/i/k", $bin);

say Bt16($aa);
say Bt($bb);
say Bt64($cc);
// PRINTS
// 20526
// 1347307310
// 4851312677560274252
// P.
// PNG.
// CSS-HTML
// 2E 50 2E 47 4E 50 43 53 53 2D 48 54 4D 4C -- -- .P.GNPCSS-HTML
// P.
// PNG.
// CSS-HTML

Check a magic number:

// pack the data
my $data = Pack("Nz0", Tb("PNG."), "Hello World!");
// print it
say BinaryExpand($bin, false);

// extract magic number
my $magicNumber = $data[:4];

// check magic number
if (Bt($magicNumber) == "PNG.")
{
    say "Magic number was correct!";
    say "Data is: " . UnpackSingle("z0", $data, 4);
}
else
    say "Magic number was incorrect!";

// PRINTS
// Magic number was correct!
// Data is: Hello World!

 


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