当前位置: 首页>>代码示例>>PHP>>正文


PHP Math_BigInteger::mode方法代码示例

本文整理汇总了PHP中Math_BigInteger::mode方法的典型用法代码示例。如果您正苦于以下问题:PHP Math_BigInteger::mode方法的具体用法?PHP Math_BigInteger::mode怎么用?PHP Math_BigInteger::mode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Math_BigInteger的用法示例。


在下文中一共展示了Math_BigInteger::mode方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: __construct

 /**
  * Converts base-2, base-10, base-16, and binary strings (eg. base-256) to BigIntegers.
  *
  * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using
  * two's compliment.  The sole exception to this is -10, which is treated the same as 10 is.
  *
  * Here's an example:
  * <code>
  * <?php
  *    include('Math/BigInteger.php');
  *
  *    $a = new Math_BigInteger('0x32', 16); // 50 in base-16
  *
  *    echo $a->toString(); // outputs 50
  * ?>
  * </code>
  *
  * @param string[optional] $x base-10 number or base-$base number if $base set.
  * @param integer[optional] $base
  * @return Math_BigInteger
  * @access public
  */
 public function __construct($x = 0, $base = 10)
 {
     if (null === self::$mode) {
         switch (true) {
             case extension_loaded('gmp'):
                 self::$mode = Math_BigInteger::MODE_GMP;
                 break;
             case extension_loaded('bcmath'):
                 self::$mode = Math_BigInteger::MODE_BCMATH;
                 break;
             default:
                 self::$mode = Math_BigInteger::MODE_INTERNAL;
         }
     }
     switch (self::$mode) {
         case Math_BigInteger::MODE_GMP:
             if (is_resource($x) && get_resource_type($x) == 'GMP integer') {
                 $this->value = $x;
                 return;
             }
             $this->value = gmp_init(0);
             break;
         case Math_BigInteger::MODE_BCMATH:
             $this->value = '0';
             break;
         default:
             $this->value = array();
     }
     if (empty($x)) {
         return;
     }
     switch ($base) {
         case -256:
             if (ord($x[0]) & 0x80) {
                 $x = ~$x;
                 $this->is_negative = true;
             }
         case 256:
             switch (self::$mode) {
                 case Math_BigInteger::MODE_GMP:
                     $sign = $this->is_negative ? '-' : '';
                     $this->value = gmp_init($sign . '0x' . bin2hex($x));
                     break;
                 case Math_BigInteger::MODE_BCMATH:
                     // round $len to the nearest 4 (thanks, DavidMJ!)
                     $len = strlen($x) + 3 & 0xfffffffc;
                     $x = str_pad($x, $len, chr(0), STR_PAD_LEFT);
                     for ($i = 0; $i < $len; $i += 4) {
                         $this->value = bcmul($this->value, '4294967296', 0);
                         // 4294967296 == 2**32
                         $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + (ord($x[$i + 1]) << 16 | ord($x[$i + 2]) << 8 | ord($x[$i + 3])), 0);
                     }
                     if ($this->is_negative) {
                         $this->value = '-' . $this->value;
                     }
                     break;
                     // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
                 // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
                 default:
                     while (strlen($x)) {
                         $this->value[] = $this->_bytes2int($this->_base256_rshift($x, 26));
                     }
             }
             if ($this->is_negative) {
                 if (self::$mode != Math_BigInteger::MODE_INTERNAL) {
                     $this->is_negative = false;
                 }
                 $temp = $this->add(new Math_BigInteger('-1'));
                 $this->value = $temp->value;
             }
             break;
         case 16:
         case -16:
             if ($base > 0 && $x[0] == '-') {
                 $this->is_negative = true;
                 $x = substr($x, 1);
             }
             $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x);
//.........这里部分代码省略.........
开发者ID:tomi77,项目名称:sfPEARcaptchaPlugin,代码行数:101,代码来源:BigInteger.php


注:本文中的Math_BigInteger::mode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。