Warning: Cannot use a scalar value as an array in /home/admin/public_html/forum/include/fm.class.php on line 757

Warning: Invalid argument supplied for foreach() in /home/admin/public_html/forum/include/fm.class.php on line 770
Форумы портала PHP.SU :: Версия для печати :: Конвертор
Форумы портала PHP.SU » PHP » Пользовательские функции » Конвертор

Страниц (1): [1]
 

1. EuGen - 05 Мая, 2009 - 15:17:22 - перейти к сообщению
Приведу класс, который написал, как ответ на вопрос из урока номер 5 (о математических функциях).
Предназначен для перевода числа из одной системы счисления в другую. Отличие от base_convert в том, что основания обеих систем не ограничены (можно, в принципе, хоть как в unicode 65536 сделать)
PHP:
скопировать код в буфер обмена
  1.  
  2. <?PHP
  3. class Converter
  4. {
  5.         protected $cipherSet;
  6.         protected $rgCiphers;
  7.    
  8.         protected $inputSet;
  9.         protected $outputSet;
  10.         protected $fromBase;
  11.         protected $toBase;
  12.  
  13.        
  14.         protected $iErrorCode;
  15.         protected $sErrorMessage;
  16.        
  17.         /*Submit only full cipher set*/
  18.         function __construct($strSet)
  19.         {
  20.                 $this->cipherSet=$strSet;
  21.                 $this->iErrorCode=0;
  22.                 $this->sErrorMessage="";
  23.         }
  24.         /*multiplication with non-decimal base*/
  25.         protected function baseArithmeticMult($num)
  26.         {
  27.                 $currentResultLen=count($this->rgCiphers);
  28.                 if ($currentResultLen==0)
  29.                 {
  30.                         return;
  31.                 }
  32.                 $rgMods=array();
  33.                 $currentPos=0;
  34.                 $div=0;
  35.                 do
  36.                 {
  37.                         $divided=0;
  38.                         if ($currentResultLen>$currentPos)
  39.                         {
  40.                                 $divided=$this->rgCiphers[$currentPos]*$num;
  41.                         }
  42.                         $divided+=$div;
  43.                         $rgMods[$currentPos]=$divided%$this->toBase;
  44.                         $div=(int)($divided/$this->toBase);
  45.                         $currentPos++;
  46.                 }
  47.                 while($currentResultLen>$currentPos||$div!=0);
  48.                 $this->rgCiphers=$rgMods;
  49.         }
  50.         /*addition with non-decimal base*/
  51.         protected function baseArithmeticPlus($num)
  52.         {
  53.                 $currentPos=0;
  54.                 $divided=$num;
  55.                 do
  56.                 {
  57.                         $divided+=(int)($this->rgCiphers[$currentPos]);
  58.                         $this->rgCiphers[$currentPos]=$divided%$this->toBase;
  59.                         $divided=(int)($divided/$this->toBase);
  60.                         $currentPos++;
  61.                 }
  62.                 while ($buf>0);
  63.         }
  64.         /*error's getters*/
  65.         public function getErrorCode()
  66.         {
  67.                 return $this->iErrorCode;
  68.         }
  69.        
  70.         public function getErrorMessage()
  71.         {
  72.                 return $this->sErrorMessage;
  73.         }
  74.         /*main function*/
  75.         public function baseConvert($num, $fromBase=0, $toBase=0)
  76.         {
  77.                 $num=(string)($num);
  78.                 $totalCiphers=strlen($num);
  79.                 $this->inputSet=substr($this->cipherSet, 0, $fromBase);
  80.                 $this->outputSet=substr($this->cipherSet, 0, $toBase);
  81.                 $this->toBase=$toBase;
  82.                 $this->rgCiphers=array();
  83.                 for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++)
  84.                 {
  85.                         $currentCipher=$num[$currentPos];
  86.                         $cipherSign=substr_count($this->inputSet, $currentCipher);
  87.                         if($cipherSign==0)
  88.                         {
  89.                                 $this->iErrorCode=255;
  90.                                 $this->sErrorMessage="Cipher '".$currentCipher."' was not found in cipher set: ".$this->inputSet;
  91.                                 return null;
  92.                         }
  93.                         elseif($cipherSign>1)
  94.                         {
  95.                                 $this->iErrorCode=255;
  96.                                 $this->sErrorMessage="Cipher '".$currentCipher."' was found more than once in cipher set: ".$this->inputSet;
  97.                                 return null;
  98.                         }
  99.                         if ($currentPos!=0)
  100.                         {
  101.                                 $this->baseArithmeticMult($fromBase);
  102.                         }
  103.                         $this->baseArithmeticPlus(strpos($this->inputSet, $currentCipher));
  104.                 }
  105.                 $convertedNum='';
  106.                 $totalCiphers=count($this->rgCiphers);
  107.                 for ($currentPos=0; $currentPos<$totalCiphers; $currentPos++)
  108.                 {
  109.                         $convertedNum=$this->outputSet[$this->rgCiphers[$currentPos]].$convertedNum;
  110.                 }
  111.                 return $convertedNum;
  112.         }
  113. }
  114. ?>
  115.  

 

Powered by ExBB FM 1.0 RC1