note 109414 added to function.bindec
| From: | patrick dot boens at latosensu dot be | Date: | Sun, 15 Jul 2012 13:14:04 +0000 |
| Subject: | note 109414 added to function.bindec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-189145@lists.php.net to get a copy of this message | ||
Left-shift a string by a number of bytes
<?php
function STR_shl( $szStr,$nBits )
/*-----------------------------*/
{
if ( $nBits < 1 || $nBits > 7 ) /* If not adequate
number of bits */
{
return ( $szStr ); /* Return the original
string */
} /* if ( $nBits < 1 || $nBits > 7 ) */
if ( ( $iLength = strlen( $szStr ) ) <= 0 ) /* If string empty */
{
return ( $szStr ); /* Return the original
string */
} /* if ( ( $iLength = strlen( $szStr ) ) <= 0 ) */
$szRetVal = ''; /* Create an empty
string */
$szBits = STR_Binary( $szStr ); /* $szStr in bits */
$szLostBits = STR_Left( $szBits,$nBits ); /* The $nBits leftmost bits
of the string */
$szShifted = substr( $szBits,$nBits ) . $szLostBits; /* $szStr left shifted */
for ( $i = 0;$i < $iLength;$i++ ) /* Treat the entire
string (per slice of 8 bits) */
{
$szRetVal .= chr( bindec( substr( $szShifted,$i * 8,8 ) ) ); /* Concatenate the CHR to
the result string */
} /* for ( $i = 0;$i < $iLength;$i++ ) */
return ( $szRetVal ); /* Return result to caller
*/
}
?>
Right-shift a string by a number of bytes
<?php
function STR_shr( $szStr,$nBits )
/*-----------------------------*/
{
if ( $nBits < 1 || $nBits > 7 ) /* If not adequate
number of bits */
{
return ( $szStr ); /* Return the original
string */
} /* if ( $nBits < 1 || $nBits > 7 ) */
if ( ( $iLength = strlen( $szStr ) ) <= 0 ) /* If string empty */
{
return ( $szStr ); /* Return the original
string */
} /* if ( ( $iLength = strlen( $szStr ) ) <= 0 ) */
$szRetVal = ''; /* Create an empty
string */
$szBits = STR_Binary( $szStr ); /* $szStr in bits */
$szLostBits = STR_Right( $szBits,$nBits ); /* The $nBits rightmost bits
of the string */
$szShifted = $szLostBits . substr( $szBits,0,-$nBits ); /* $szStr right shifted */
for ( $i = 0;$i < $iLength;$i++ ) /* Treat the entire
string (per slice of 8 bits) */
{
$szRetVal .= chr( bindec( substr( $szShifted,$i * 8,8 ) ) ); /* Concatenate the CHR to
the result string */
} /* for ( $i = 0;$i < $iLength;$i++ ) */
return ( $szRetVal ); /* Return result to caller
*/
}
?>
Additional functions used by the two preceding:
<?php
function STR_Binary( $szStr )
/*-------------------------*/
{
$szRetVal = ''; /* Ready to return
an empty string */
if ( ( $iLength = strlen( $szStr ) ) > 0 ) /* If string NOT empty */
{
for ( $i = 0; $i < $iLength;$i++ ) /* Treat each character
of the string */
{
$szRetVal .= sprintf( '%08b',ord( $szStr[$i] ) ); /* Turn this char
to a binary representation (8 bits) */
} /* for ( $i = 0; $i < $iLength;$i++ ) */
} /* if ( ( $iLength = strlen( $szStr ) ) > 0 ) */
return ( $szRetVal ); /* Return result to caller
*/
}
function STR_Left( $szStr,$iCount = 1 )
/*----------------------------------*/
{
return substr( $szStr,0,$iCount );
} /* End of function strleft() */
function STR_Right( $szString,$iCount )
/*----------------------------------*/
{
return substr( $szString,0 + strlen( $szString ) - $iCount,$iCount );
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 81.244.174.33
----
Manual Page -- http://www.php.net/manual/en/function.bindec.php
Edit -- https://master.php.net/note/edit/109414
Del: integrated -- https://master.php.net/note/delete/109414/integrated
Del: useless -- https://master.php.net/note/delete/109414/useless
Del: bad code -- https://master.php.net/note/delete/109414/bad+code
Del: spam -- https://master.php.net/note/delete/109414/spam
Del: non-english -- https://master.php.net/note/delete/109414/non-english
Del: in docs -- https://master.php.net/note/delete/109414/in+docs
Del: other reasons-- https://master.php.net/note/delete/109414
Reject -- https://master.php.net/note/reject/109414
Search -- https://master.php.net/manage/user-notes.php