Jump to content

Bit mask applied to character?


miked2

Recommended Posts

I have the following JavaScript code:

 

// mask = 0xFF or bin "11111111" or dec 255.

var strChar = str.charCodeAt(i);

var maskedChar = strChar & mask;

 

I need to translate that into PHP:

 

$strChar = $str[$i];

$maskedChar = $strChar & mask;

 

The second line of PHP doesn't work - it always returns a binary 0.

 

How do you do a bit mask of a character in PHP?  According to something I found in the PHP bug reporting site (bugs.php.net), you can't do this period - someone decided that it doesn't make sense to perform bitwise operations on strings.  Is this still true?  Is there a work-around?  Clearly, there's a use for bitwise operations on characters, since that JavaScript code is widely used for encryption/decryption of passwords etc.

 

PS - the character can be either single or double byte and the mask removes the high byte.

Link to comment
https://forums.phpfreaks.com/topic/114337-bit-mask-applied-to-character/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.