Bitpay\Util\Util::decToBin PHP Метод

decToBin() публичный статический Метод

This method returns a binary string representation of the decimal number. Used for the doubleAndAdd() method.
См. также: http://php.net/manual/en/function.decbin.php but for large numbers
public static decToBin ( $dec ) : string
Результат string
    public static function decToBin($dec)
    {
        if (substr(strtolower($dec), 0, 2) == '0x') {
            $dec = self::decodeHex(substr($dec, 2));
        }
        $bin = '';
        while (Math::cmp($dec, '0') > 0) {
            if (Math::mod($dec, 2) == '1') {
                $bin .= '1';
            } else {
                $bin .= '0';
            }
            $prevDec = $dec;
            $dec = Math::div($dec, 2);
            //sanity check to avoid infinite loop
            if (Math::cmp($prevDec, $dec) < 1) {
                throw new \Exception('Math library has unexpected behavior, please report the following information to [email protected]. Math Engine is: ' . Math::getEngineName() . '. PHP Version is: ' . phpversion() . '.');
            }
        }
        return $bin;
    }

Usage Example

Пример #1
0
 public function testDecToBin()
 {
     $data = array(array('123456789', '101010001011001111011010111'), array('0x123456789', '100100011110011010100010110001001'));
     foreach ($data as $datum) {
         $this->assertSame($datum[1], Util::decToBin($datum[0]));
     }
 }