private static function _encodeByteStr($byteStr, $alphabet, $pad) {
        // Check if argument is a string.
        if (!is_string($byteStr)) {
            $msg = 'Supplied argument 1 is not a string.';
            throw new InvalidArgumentException($msg, self::E_NO_STRING);
        }

        // Get byte count.
        $byteCount = self::_byteCount($byteStr);

        // Make byte count divisible by 5.
        $remainder = $byteCount % 5;
        $fillbyteCount = ($remainder) ? 5 - $remainder : 0;
        if ($fillbyteCount > 0)
            $byteStr .= str_repeat(chr(0), $fillbyteCount);

        // Iterate over blocks of 5 bytes and build encoded string.
        $encodedStr = '';
        for ($i = 0; $i < ($byteCount + $fillbyteCount); $i = $i + 5) {
            // Convert chars to bytes.
            $byte1 = ord($byteStr[$i]);
            $byte2 = ord($byteStr[$i + 1]);
            $byte3 = ord($byteStr[$i + 2]);
            $byte4 = ord($byteStr[$i + 3]);
            $byte5 = ord($byteStr[$i + 4]);
            // Read first 5 bit group.
            $bitGroup = $byte1 >> 3;
            $encodedStr .= $alphabet[$bitGroup];
            // Read second 5 bit group.
            $bitGroup = ($byte1 & ~(31 << 3)) << 2 | $byte2 >> 6;
            $encodedStr .= $alphabet[$bitGroup];
            // Read third 5 bit group.
            $bitGroup = $byte2 >> 1 & ~(3 << 5);
            $encodedStr .= $alphabet[$bitGroup];
            // Read fourth 5 bit group.
            $bitGroup = ($byte2 & 1) << 4 | $byte3 >> 4;
            $encodedStr .= $alphabet[$bitGroup];
            // Read fifth 5 bit group.
            $bitGroup = ($byte3 & ~(15 << 4)) << 1 | $byte4 >> 7;
            $encodedStr .= $alphabet[$bitGroup];
            // Read sixth 5 bit group.
            $bitGroup = $byte4 >> 2 & ~(1 << 5);
            $encodedStr .= $alphabet[$bitGroup];
            // Read seventh 5 bit group.
            $bitGroup = ($byte4 & ~(63 << 2)) << 3 | $byte5 >> 5;
            $encodedStr .= $alphabet[$bitGroup];
            // Read eighth 5 bit group.
            $bitGroup = $byte5 & ~(7 << 5);
            $encodedStr .= $alphabet[$bitGroup];
        }

        // Replace fillbit characters at the end of the encoded string.
        $encodedStrLen = ($byteCount + $fillbyteCount) * 8 / 5;
        $fillbitCharCount = (int) ($fillbyteCount * 8 / 5);
        $encodedStr = substr($encodedStr, 0, $encodedStrLen - $fillbitCharCount);
        if ($pad)
            $encodedStr .= str_repeat($alphabet[32], $fillbitCharCount);

        // Return encoded string.
        return $encodedStr;
    }
private static function _encodeByteStr($byteStr, $alphabet, $pad) {
        // Check if argument is a string.
        if (!is_string($byteStr)) {
            $msg = 'Supplied argument 1 is not a string.';
            throw new InvalidArgumentException($msg, self::E_NO_STRING);
        }

        // Get byte count.
        $byteCount = self::_byteCount($byteStr);

        // Make byte count divisible by 5.
        $remainder = $byteCount % 5;
        $fillbyteCount = ($remainder) ? 5 - $remainder : 0;
        if ($fillbyteCount > 0)
            $byteStr .= str_repeat(chr(0), $fillbyteCount);

        // Iterate over blocks of 5 bytes and build encoded string.
        $encodedStr = '';
        for ($i = 0; $i < ($byteCount + $fillbyteCount); $i = $i + 5) {
            // Convert chars to bytes.
            $byte1 = ord($byteStr[$i]);
            $byte2 = ord($byteStr[$i + 1]);
            $byte3 = ord($byteStr[$i + 2]);
            $byte4 = ord($byteStr[$i + 3]);
            $byte5 = ord($byteStr[$i + 4]);
            // Read first 5 bit group.
            $bitGroup = $byte1 >> 3;
            $encodedStr .= $alphabet[$bitGroup];
            // Read second 5 bit group.
            $bitGroup = ($byte1 & ~(31 << 3)) << 2 | $byte2 >> 6;
            $encodedStr .= $alphabet[$bitGroup];
            // Read third 5 bit group.
            $bitGroup = $byte2 >> 1 & ~(3 << 5);
            $encodedStr .= $alphabet[$bitGroup];
            // Read fourth 5 bit group.
            $bitGroup = ($byte2 & 1) << 4 | $byte3 >> 4;
            $encodedStr .= $alphabet[$bitGroup];
            // Read fifth 5 bit group.
            $bitGroup = ($byte3 & ~(15 << 4)) << 1 | $byte4 >> 7;
            $encodedStr .= $alphabet[$bitGroup];
            // Read sixth 5 bit group.
            $bitGroup = $byte4 >> 2 & ~(1 << 5);
            $encodedStr .= $alphabet[$bitGroup];
            // Read seventh 5 bit group.
            $bitGroup = ($byte4 & ~(63 << 2)) << 3 | $byte5 >> 5;
            $encodedStr .= $alphabet[$bitGroup];
            // Read eighth 5 bit group.
            $bitGroup = $byte5 & ~(7 << 5);
            $encodedStr .= $alphabet[$bitGroup];
        }

        // Replace fillbit characters at the end of the encoded string.
        $encodedStrLen = ($byteCount + $fillbyteCount) * 8 / 5;
        $fillbitCharCount = (int) ($fillbyteCount * 8 / 5);
        $encodedStr = substr($encodedStr, 0, $encodedStrLen - $fillbitCharCount);
        if ($pad)
            $encodedStr .= str_repeat($alphabet[32], $fillbitCharCount);

        // Return encoded string.
        return $encodedStr;
    }
private static function _encodeByteStr ( $ byteStr , $ alphabet , $ pad ) { // Check if argument is a string. if ( ! is_string ( $ byteStr ) ) { $ msg = 'Supplied argument 1 is not a string.' ; throw new InvalidArgumentException ( $ msg , self :: E_NO_STRING ) ; } // Get byte count. $ byteCount = self :: _byteCount ( $ byteStr ) ; // Make byte count divisible by 5. $ remainder = $ byteCount % 5 ; $ fillbyteCount = ( $ remainder ) ? 5 - $ remainder : 0 ; if ( $ fillbyteCount > 0 ) $ byteStr .= str_repeat ( chr ( 0 ) , $ fillbyteCount ) ; // Iterate over blocks of 5 bytes and build encoded string. $ encodedStr = '' ; for ( $ i = 0 ; $ i < ( $ byteCount + $ fillbyteCount ) ; $ i = $ i + 5 ) { // Convert chars to bytes. $ byte1 = ord ( $ byteStr [ $ i ] ) ; $ byte2 = ord ( $ byteStr [ $ i + 1 ] ) ; $ byte3 = ord ( $ byteStr [ $ i + 2 ] ) ; $ byte4 = ord ( $ byteStr [ $ i + 3 ] ) ; $ byte5 = ord ( $ byteStr [ $ i + 4 ] ) ; // Read first 5 bit group. $ bitGroup = $ byte1 >> 3 ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read second 5 bit group. $ bitGroup = ( $ byte1 & ~ ( 31 << 3 ) ) << 2 | $ byte2 >> 6 ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read third 5 bit group. $ bitGroup = $ byte2 >> 1 & ~ ( 3 << 5 ) ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read fourth 5 bit group. $ bitGroup = ( $ byte2 & 1 ) << 4 | $ byte3 >> 4 ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read fifth 5 bit group. $ bitGroup = ( $ byte3 & ~ ( 15 << 4 ) ) << 1 | $ byte4 >> 7 ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read sixth 5 bit group. $ bitGroup = $ byte4 >> 2 & ~ ( 1 << 5 ) ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read seventh 5 bit group. $ bitGroup = ( $ byte4 & ~ ( 63 << 2 ) ) << 3 | $ byte5 >> 5 ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; // Read eighth 5 bit group. $ bitGroup = $ byte5 & ~ ( 7 << 5 ) ; $ encodedStr .= $ alphabet [ $ bitGroup ] ; } // Replace fillbit characters at the end of the encoded string. $ encodedStrLen = ( $ byteCount + $ fillbyteCount ) * 8 / 5 ; $ fillbitCharCount = ( int ) ( $ fillbyteCount * 8 / 5 ) ; $ encodedStr = substr ( $ encodedStr , 0 , $ encodedStrLen - $ fillbitCharCount ) ; if ( $ pad ) $ encodedStr .= str_repeat ( $ alphabet [ 32 ] , $ fillbitCharCount ) ; // Return encoded string. return $ encodedStr ; }
Base 32 encodes the given byte-string using the given alphabet.

@param string $byteStr String containing the bytes to be encoded.
@param string $alphabet The alphabet to be used for encoding.
@param bool $pad If true, the encoded string is padded using the padding
character specified in the given alphabet to have a length which
is evenly divisible by 8.
@return string The encoded string.
@throws InvalidArgumentException
Base 32 encodes the given byte - string using the given alphabet .