Ejemplo n.º 1
0
 public static function str2num($str)
 {
     switch (BigInt::support()) {
         case 'gmp':
             return gmp_init("0x" . bin2hex($str));
         case 'big_int':
             return bi_from_str(bin2hex($str), 16);
         case 'bcmath':
             bcscale(0);
             $len = strlen($str);
             $result = '0';
             $m = '1';
             for ($i = 0; $i < $len; $i++) {
                 $result = bcadd(bcmul($m, ord($str[$len - $i - 1])), $result);
                 $m = bcmul($m, '256');
             }
             return $result;
         case '':
         default:
             return BigInt::_str2num($str);
     }
 }
Ejemplo n.º 2
0
 function bigint_str2num($str)
 {
     return bi_from_str(bin2hex($str), 16);
 }
Ejemplo n.º 3
0
<?php

$two = bi_from_str('2');
$four = bi_add($two, $two);
// Use bi_to_str() to get strings from big_int resources
print bi_to_str($four);
// prints 4
// Computing large factorials very quickly
$factorial = bi_fact(20);
Ejemplo n.º 4
0
echo '<h1>BIG_INT test and example of usage</h1>' . "\n";
// assign a big number to $a
$a = bi_from_str('12345678901234567890');
// wiew, what returns var_dump($a) (it is NOT a simple integer, but BIG_INT resource)
echo 'var_dump($a) = ', "<br/>\n";
var_dump($a);
echo "<br/>\n";
// now show decimal value of $a
echo '$a = [', bi_to_str($a), "]<br/>\n";
/*
    second argument of bi_from_str() and bi_to_str() - is the base
    of string representation of number. It can be from 2 to 36 inclusive
*/
$str = 'abcdef012789';
echo 'hex value of $str = [', $str, "]<br/>\n";
$b = bi_from_str($str, 16);
echo 'decimal value of $b = [', bi_to_str($b), "]<br/>\n";
echo 'binary value of $b = [', bi_to_str($b, 2), "]<br/>\n";
/*
    string bi_base_convert(string $num, int $base_from, int $base_to)

    Converts string representation of $num from base $base_from to $base_to
*/
$num = '-12345678900987654321';
echo 'Decimal number [', $num, '] equals to [', bi_base_convert($num, 10, 36), '] by base 36', "<br/>\n";
/*
    basic functions
*/
echo '<h3>basic functions</h3>' . "\n";
$c = bi_add($a, $b);
echo '$a + $b = [', bi_to_str($c), "]<br/>\n";
Ejemplo n.º 5
0
    modify it freely. You can use it with any free or commercial
    software.

    These sources is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY. Without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    You may contact the author by:
       e-mail:  valyala@gmail.com
*************************************************************************/
/**
    tests bitset functions (or, and, xor, andnot with start position displacement)
*/
require_once dirname(__FILE__) . '/std_header.php';
$a = bi_from_str('1110110110110101011000100010110011101110111', 2);
$b = bi_from_str('11010010101111101010000101010100011101', 2);
echo '$a = ', bi_to_str($a, 2), "\n";
echo '$b = ', bi_to_str($b, 2), "\n";
////////////////////////////////////////////////
$c = bi_andnot($a, $b);
echo 'bi_andnot($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_xor($a, $b);
echo 'bi_xor($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_or($a, $b);
echo 'bi_or($a, $b) = ', bi_to_str($c, 2), "\n";
$c = bi_and($a, $b);
echo 'bi_and($a, $b) = ', bi_to_str($c, 2), "\n";
////////////////////////////////////////////////
$c = bi_andnot($a, $b, 2);
echo 'bi_andnot($a, $b, 2) = ', bi_to_str($c, 2), "\n";
$c = bi_xor($a, $b, 5);