Convert from PHP Bitwise + Modulus Operator

I have this function in PHP

for ($i = 0; $i < $payloadLength; $i++) {
   $frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i];
}

Or to make things just a little bit less complex, the following returns the same:

if ($masked == true) { 
	$result = $payload[$i] ^ $mask[$i % 4];
} else {
	$result = $payload[$i];
}
$frame .= $result;

And I am focused on this line:

$result = $payload[$i] ^ $mask[$i % 4];

The ^ symbol represents the XOR BitWise and the % is a Modulus Arithmetic Operator but I haven’t found this Modulus feature in Xojo Manual and the BitWise is a little bit tricky to be honest. Also, according the manual BitWise is expecting an Integer ? Is that right ?

Could someone with BitWise experience assist me in translating this in to Xojo language ?

The $payload is a String (i.e: “Hello Word!”) and with the [$i] in front of it, it will catch that character space, one by one, like Mid in Xojo
The $mask is an array(3) with some a random Chrs from 0 to 255… but I don’t know how to obtain the % same result in Xojo.

So far, my best guess is (incomplete):

Dim result As String result = payload(payload.Mid(i, i+1)) ^ mask(i % 4)

mod
http://documentation.xojo.com/index.php/Mod

result=payload(i) xor mask(i mod 4)

Dave, that simple ? :s
Thanks :smiley:

Note that there is a difference between Bitwise.BitXOR() vs just plain Xor - the former uses 64 bit ints, can take any number of parameters, and is much slower than the latter. I think using Xor in this case is the right choice.

Oh no! Type mismatch error. Expected Boolean, but got String

result = payload.Mid(y, 1) Xor mask(y Mod 4)

yeah… you dim’d result as string, and it should be integer

Dave, doesn’t work either. Expected Boolean, but got Int32

result = Asc(payload.Mid(y, 1)) Xor mask(y Mod 4)

sorry… I meant boolean :slight_smile:

Manual says Boolean or Integer :s

depends… what datatype is MASK()

Got it! result was a String :s sorry