- Модуль: main
- Путь к файлу: ~/bitrix/modules/main/lib/text/base32.php
- Класс: BitrixMainTextBase32
- Вызов: Base32::encode
static function encode($string)
{
if (!$string)
return '';
if (!is_string($string))
throw new ArgumentTypeException('string', 'string');
if (self::$encodeAlphabet === null)
self::$encodeAlphabet = array_flip(self::$alphabet);
// Convert string to binary
$binaryString = '';
foreach (str_split($string) as $s)
{
// Return each character as an 8-bit binary string
$s = decbin(ord($s));
$binaryString .= str_pad($s, 8, 0, STR_PAD_LEFT);
}
// Break into 5-bit chunks, then break that into an array
$binaryArray = self::chunk($binaryString, 5);
// Pad array to be divisible by 8
while (count($binaryArray) % 8 !== 0)
{
$binaryArray[] = null;
}
$base32String = '';
// Encode in base32
foreach ($binaryArray as $bin)
{
$char = 32;
if (!is_null($bin))
{
// Pad the binary strings
$bin = str_pad($bin, 5, 0, STR_PAD_RIGHT);
$char = bindec($bin);
}
// Base32 character
$base32String .= self::$encodeAlphabet[$char];
}
return $base32String;
}