Приведу класс, который написал, как ответ на вопрос из урока номер 5 (о математических функциях).
Предназначен для перевода числа из одной системы счисления в другую. Отличие от base_convert в том, что основания обеих систем не ограничены (можно, в принципе, хоть как в unicode 65536 сделать)
PHP:
скопировать код в буфер обмена
<?PHP
class Converter
{
protected $cipherSet ;
protected $rgCiphers ;
protected $inputSet ;
protected $outputSet ;
protected $fromBase ;
protected $toBase ;
protected $iErrorCode ;
protected $sErrorMessage ;
/*Submit only full cipher set*/
function __construct( $strSet )
{
$this -> cipherSet = $strSet ;
$this -> iErrorCode = 0 ;
$this -> sErrorMessage = "" ;
}
/*multiplication with non-decimal base*/
protected function baseArithmeticMult( $num )
{
$currentResultLen = count ( $this -> rgCiphers ) ; if ( $currentResultLen == 0)
{
return ;
}
$currentPos = 0 ;
$div = 0 ;
do
{
$divided = 0 ;
if ( $currentResultLen > $currentPos )
{
$divided = $this -> rgCiphers [ $currentPos ] * $num ;
}
$divided += $div ;
$rgMods [ $currentPos ] = $divided % $this -> toBase ;
$div = ( int) ( $divided / $this -> toBase ) ;
$currentPos ++;
}
while ( $currentResultLen > $currentPos || $div != 0) ;
$this -> rgCiphers = $rgMods ;
}
/*addition with non-decimal base*/
protected function baseArithmeticPlus( $num )
{
$currentPos = 0 ;
$divided = $num ;
do
{
$divided += ( int) ( $this -> rgCiphers [ $currentPos ] ) ;
$this -> rgCiphers [ $currentPos ] = $divided % $this -> toBase ;
$divided = ( int) ( $divided / $this -> toBase ) ;
$currentPos ++;
}
while ( $buf > 0) ;
}
/*error's getters*/
public function getErrorCode( )
{
return $this -> iErrorCode ;
}
public function getErrorMessage( )
{
return $this -> sErrorMessage ;
}
/*main function*/
public function baseConvert( $num , $fromBase = 0, $toBase = 0)
{
$num = ( string) ( $num ) ;
$this -> inputSet = substr ( $this -> cipherSet , 0
, $fromBase ) ; $this -> outputSet = substr ( $this -> cipherSet , 0
, $toBase ) ; $this -> toBase = $toBase ;
$this -> rgCiphers = array ( ) ; for ( $currentPos = 0 ; $currentPos < $totalCiphers ; $currentPos ++ )
{
$currentCipher = $num [ $currentPos ] ;
if ( $cipherSign == 0)
{
$this -> iErrorCode = 255 ;
$this -> sErrorMessage = "Cipher '" . $currentCipher . "' was not found in cipher set: " . $this -> inputSet ;
return null ;
}
elseif ( $cipherSign > 1)
{
$this -> iErrorCode = 255 ;
$this -> sErrorMessage = "Cipher '" . $currentCipher . "' was found more than once in cipher set: " . $this -> inputSet ;
return null ;
}
if ( $currentPos != 0)
{
$this -> baseArithmeticMult ( $fromBase ) ;
}
$this -> baseArithmeticPlus ( strpos ( $this -> inputSet , $currentCipher ) ) ; }
$convertedNum = '' ;
$totalCiphers = count ( $this -> rgCiphers ) ; for ( $currentPos = 0 ; $currentPos < $totalCiphers ; $currentPos ++ )
{
$convertedNum = $this -> outputSet [ $this -> rgCiphers [ $currentPos ] ] . $convertedNum ;
}
return $convertedNum ;
}
}
?>
-----Есть в мире две бесконечные вещи - это Вселенная и человеческая глупость. Но насчет первой .. я не уверен.