miked2 Posted July 11, 2008 Share Posted July 11, 2008 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 More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 You need to use ord() to get the ASCII equivalent of the character. Link to comment https://forums.phpfreaks.com/topic/114337-bit-mask-applied-to-character/#findComment-587954 Share on other sites More sharing options...
miked2 Posted July 11, 2008 Author Share Posted July 11, 2008 Great - that did it! Thanks! Mike Link to comment https://forums.phpfreaks.com/topic/114337-bit-mask-applied-to-character/#findComment-588039 Share on other sites More sharing options...
DarkWater Posted July 11, 2008 Share Posted July 11, 2008 No problem. =) Please mark this topic as solved. Link to comment https://forums.phpfreaks.com/topic/114337-bit-mask-applied-to-character/#findComment-588046 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.