Left-shift a string by a number of bytes
<?php
function STR_shl( $szStr,$nBits )
{
if ( $nBits < 1 || $nBits > 7 ) {
return ( $szStr ); } if ( ( $iLength = strlen( $szStr ) ) <= 0 ) {
return ( $szStr ); } $szRetVal = ''; $szBits = STR_Binary( $szStr ); $szLostBits = STR_Left( $szBits,$nBits ); $szShifted = substr( $szBits,$nBits ) . $szLostBits; for ( $i = 0;$i < $iLength;$i++ ) {
$szRetVal .= chr( bindec( substr( $szShifted,$i * 8,8 ) ) ); } return ( $szRetVal ); }
?>
Right-shift a string by a number of bytes
<?php
function STR_shr( $szStr,$nBits )
{
if ( $nBits < 1 || $nBits > 7 ) {
return ( $szStr ); } if ( ( $iLength = strlen( $szStr ) ) <= 0 ) {
return ( $szStr ); } $szRetVal = ''; $szBits = STR_Binary( $szStr ); $szLostBits = STR_Right( $szBits,$nBits ); $szShifted = $szLostBits . substr( $szBits,0,-$nBits ); for ( $i = 0;$i < $iLength;$i++ ) {
$szRetVal .= chr( bindec( substr( $szShifted,$i * 8,8 ) ) ); } return ( $szRetVal ); }
?>
Additional functions used by the two preceding:
<?php
function STR_Binary( $szStr )
{
$szRetVal = ''; if ( ( $iLength = strlen( $szStr ) ) > 0 ) {
for ( $i = 0; $i < $iLength;$i++ ) {
$szRetVal .= sprintf( '%08b',ord( $szStr[$i] ) ); } } return ( $szRetVal ); }
function STR_Left( $szStr,$iCount = 1 )
{
return substr( $szStr,0,$iCount );
} function STR_Right( $szString,$iCount )
{
return substr( $szString,0 + strlen( $szString ) - $iCount,$iCount );
}
?>