private static function _intStrToByteStr($intStr) {
        // Check if given value is a positive integer (string).
        if (!preg_match('/[0-9]+/', (string) $intStr)) {
            $msg = 'Argument 1 must be a non-negative integer or a string representing a non-negative integer.';
            throw new InvalidArgumentException($msg, self::E_NO_NON_NEGATIVE_INT);
        }
        $byteStr = '';
        // If possible, use integer type for conversion.
        $int = (int) $intStr;
        if ((string) $int == (string) $intStr) {
            // Use of integer type is possible.
            // Convert integer to byte-string.
            while ($int > 0) {
                $byteStr = chr($int & 255) . $byteStr;
                $int >>= 8;
            }
        } else {
            // Cannot use integer type, use BC Math library.
            if (extension_loaded('bcmath')) {
                // Convert integer to byte-string.
                while ((int) $intStr > 0) {
                    $byteStr = chr(bcmod($intStr, '256')) . $byteStr;
                    $intStr = bcdiv($intStr, '256', 0);
                }
            } else {
                throw new RuntimeException('BC Math functions are not available.', self::E_NO_BCMATH);
            }
        }
        if ($byteStr == '')
            $byteStr = chr(0);
        return $byteStr;
    }
private static function _intStrToByteStr($intStr) {
        // Check if given value is a positive integer (string).
        if (!preg_match('/[0-9]+/', (string) $intStr)) {
            $msg = 'Argument 1 must be a non-negative integer or a string representing a non-negative integer.';
            throw new InvalidArgumentException($msg, self::E_NO_NON_NEGATIVE_INT);
        }
        $byteStr = '';
        // If possible, use integer type for conversion.
        $int = (int) $intStr;
        if ((string) $int == (string) $intStr) {
            // Use of integer type is possible.
            // Convert integer to byte-string.
            while ($int > 0) {
                $byteStr = chr($int & 255) . $byteStr;
                $int >>= 8;
            }
        } else {
            // Cannot use integer type, use BC Math library.
            if (extension_loaded('bcmath')) {
                // Convert integer to byte-string.
                while ((int) $intStr > 0) {
                    $byteStr = chr(bcmod($intStr, '256')) . $byteStr;
                    $intStr = bcdiv($intStr, '256', 0);
                }
            } else {
                throw new RuntimeException('BC Math functions are not available.', self::E_NO_BCMATH);
            }
        }
        if ($byteStr == '')
            $byteStr = chr(0);
        return $byteStr;
    }
private static function _intStrToByteStr ( $ intStr ) { // Check if given value is a positive integer (string). if ( ! preg_match ( '/[0-9]+/' , ( string ) $ intStr ) ) { $ msg = 'Argument 1 must be a non-negative integer or a string representing a non-negative integer.' ; throw new InvalidArgumentException ( $ msg , self :: E_NO_NON_NEGATIVE_INT ) ; } $ byteStr = '' ; // If possible, use integer type for conversion. $ int = ( int ) $ intStr ; if ( ( string ) $ int == ( string ) $ intStr ) { // Use of integer type is possible. // Convert integer to byte-string. while ( $ int > 0 ) { $ byteStr = chr ( $ int & 255 ) . $ byteStr ; $ int >>= 8 ; } } else { // Cannot use integer type, use BC Math library. if ( extension_loaded ( 'bcmath' ) ) { // Convert integer to byte-string. while ( ( int ) $ intStr > 0 ) { $ byteStr = chr ( bcmod ( $ intStr , '256' ) ) . $ byteStr ; $ intStr = bcdiv ( $ intStr , '256' , 0 ) ; } } else { throw new RuntimeException ( 'BC Math functions are not available.' , self :: E_NO_BCMATH ) ; } } if ( $ byteStr == '' ) $ byteStr = chr ( 0 ) ; return $ byteStr ; }
Converts an integer into a byte-string.

Stores the bits representing the given non-negative integer in the
smallest sufficient number of bytes, padding the left side with zeros.
Uses PHP's BC Math library if the given integer string is too large to be
processed with PHP's internal string type.

Example: $intStr = 16706 => chr(16706 >> 8 & 255) . chr(16706 & 255) =
'AB'

@param mixed $intStr Non-negative integer or string representing a
non-negative integer.
@return string Byte-string containing the bits representing the given
integer.
@throws InvalidArgumentException
@throws RuntimeException
Converts an integer into a byte - string .