Hello all,

I'm trying to convert a function done in Delphi (pascal) to PHP. This
function is part of simple text encryption/decryption method. However, I
have a really hard time to make this work in PHP. This is the Delphi code:

Function RotateBits(C: Char; Bits: Integer): Char;
var
  SI : Word;
begin
  Bits := Bits mod 8;
  // Are we shifting left?
  if Bits < 0 then
    begin
      // Put the data on the right half of a Word (2 bytes)
      SI := MakeWord(Byte(C),0);
      // Now shift it left the appropriate number of bits
      SI := SI shl Abs(Bits);
    end
  else
    begin
      // Put the data on the left half of a Word (2 bytes)
      SI := MakeWord(0,Byte(C));
      // Now shift it right the appropriate number of bits
      SI := SI shr Abs(Bits);
    end;
  // Finally, Swap the bytes
  SI := Swap(SI);
  // And OR the two halves together
  SI := Lo(SI) or Hi(SI);
  Result := Chr(SI);
end;

and this the beginning of my attempt to do the same thing in PHP:

 function RotateBits($C, $Bits) {
  $Bits = $Bits % 8;

  if ($Bits < 0 ) {
   $C = $C << abs($Bits);
  } else {
   $C = $C >> abs($Bits);
  }

  return chr($C);
 }

In the Delphi code the MakeWord function is a macro:
#define MAKEWORD(a, b) \ ((WORD) (((BYTE) (a)) | ((WORD) ((BYTE) (b))) <<
8))
And Swap exchanges the high order byte with the low order byte of the word.

Now my question is, how can I work with bytes and words in PHP? The closest
I've found is just the integer, that may be of different sizes depending on
system. Or, as a workaround, is there any way to detect the number of bytes
an integer is on the current system?

Thanks in advance.

Best regards,
Stefan Pettersson





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to