Core Function BinaryCompare

From Sputnik Wiki
Jump to: navigation, search
BinaryCompare( <binary-array>, <offset>, <needle, <needleOffset>, <length>, <ignoreCase> )

Contents

Description

Compare two binary variables.

Parameters

binary-array

The a binary variable to use.

(Or any value that will be cast as binary)

offset

Optional; Start position to begin comparing. (0 = first byte)

If the offset is a negative value the position will work backwards from the length of the binary.

needle

The a binary variable to be comparing to.

(Or any value that will be cast as binary)

needleOffset

Optional; Start position to begin comparing in the needle. (0 = first byte)

If the needleOffset is a negative value the position will work backwards from the length of the needle binary.

length

Optional; Maximal number of bytes to compare.

Default is the entire remainder of the shortest binary variable.

If length is given and is negative, then that many bytes will be omitted from the end of binary (after the start position has been calculated when a start is negative).

If start denotes the position of this truncation or beyond, failure will be returned ( -2 ).

The length will be calculated separately for the binary and the needle then the shortest of the two will be used.

ignoreCase

Optional; if true the comparison is case-insensitive

Return Value

-1 if binary1 is less than binary2

1 if binary1 is greater than binary2

0 if they are equal.

-2 on error

Remarks

If ignoreCase is true, the result is equivalent to performing a case-sensitive comparison after converting the uppercase ASCII letters A-Z in both binary variables to lowercase.

Warning this function will compare the source to the needle to the LENGTH of the source... This means if the needle is the same as the source to the full length of the compare then you will receive a true even if immediately after that position the needle changes drastically.

The reason for that is because its checked the full size of the source against everything it can in the needle and it was all a match so naturally the compare should be true.

If you wish to make sure both variables are the same size before doing the compare check their size.

Or use the direct === to check if both binary variables are 100% the same

my $Binary1 = Pack("A*", "Hello world!!!");
my $Binary2 = Pack("A*", "Hello world!!!");
 
If( $Binary1 === $Binary2 )
{
	println("Both binary variables are exactly the same");
}
Else
{
	println("No match");
}

Example

my $Binary1 = Pack("A*", "Hello world!!!");
my $Binary2 = Pack("A*", "Hello world!!!");
 
If( BinaryCompare($Binary1, 0, $Binary2) == 0 )
{
	println("Both binary variables contain the same data");
}
Else
{
	println("No match");
}
 
println("");
println("Lets try again now now that one was changed");
my $Binary1 = Pack("A*", "Test");
If( BinaryCompare($Binary1, 0, $Binary2) == 0 )
{
	println("Both binary variables contain the same data");
}
Else
{
	println("No match");
}

Check from the 4th byte to the 6th byte

my $Binary1 = Pack("A*", "TheFoXok");
my $Binary2 = Pack("A*", "DatFoXNP");
 
If( BinaryCompare($Binary1, 0, $Binary2, 3, 3) == 0 )
{
	println("Both binary variables contain the same data");
}
Else
{
	println("No match");
}

Example of the casting

my $Binary1 = Pack("A*", "Hello world!!!");
 
If( BinaryCompare($Binary1, 0, "Hello world!!!") == 0 )
{
	println("Both binary variables contain the same data");
}
Else
{
	println("No match");
}
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox