Back to Base32 class

Method bin2str

private string
bin2str
(mixed $str)
bin2str
Parameters
  • string $str The string of 0's and 1's you want to convert
Returns
  • string The ascii output
-
  • \Exception
Class: Base32
Project: Joomla

Method bin2str - Source code

/**
 * bin2str
 *
 * Converts a binary string to an ascii string
 *
 * @param   string  $str  The string of 0's and 1's you want to convert
 *
 * @return  string  The ascii output
 *
 * @throws \Exception
 */
private function bin2str($str)
{
    if (\strlen($str) % 8 > 0) {
        throw new \Exception('Length must be divisible by 8');
    }
    if (!preg_match('/^[01]+$/', $str)) {
        throw new \Exception('Only 0\'s and 1\'s are permitted');
    }
    preg_match_all('/.{8}/', $str, $chrs);
    $chrs = array_map('bindec', $chrs[0]);
    // I'm just being slack here
    array_unshift($chrs, 'C*');
    return \call_user_func_array('pack', $chrs);
}