/** * Convert number between formats * * @param $number * @param string $inputFormat * @param string $outputFormat * @return string */ protected function convert($number, $inputFormat = self::FORMAT_NUMBER, $outputFormat = self::FORMAT_BINARY) { if ($inputFormat == $outputFormat) { return $number; } // convert to number switch ($inputFormat) { case self::FORMAT_BINARY: case self::FORMAT_BTWOC: $number = $this->math->binToInt($number); break; case self::FORMAT_NUMBER: default: // do nothing break; } // convert to output format switch ($outputFormat) { case self::FORMAT_BINARY: return $this->math->intToBin($number); break; case self::FORMAT_BTWOC: return $this->math->intToBin($number, true); break; case self::FORMAT_NUMBER: default: return $number; break; } }
/** * Decode a binary long sequence * * @param string $data * @return int|float|string */ protected function decodeBinLong($data) { $nbytes = strlen($data); if ($nbytes == 0) { return 0; } $long = 0; if ($nbytes > 7) { if ($this->bigIntegerAdapter === null) { $this->bigIntegerAdapter = BigInteger\BigInteger::getDefaultAdapter(); } if (self::$isLittleEndian === true) { $data = strrev($data); } $long = $this->bigIntegerAdapter->binToInt($data, true); } else { for ($i = 0; $i < $nbytes; $i++) { $long += ord($data[$i]) * pow(256, $i); } if (0x80 <= ord($data[$nbytes - 1])) { $long -= pow(2, $nbytes * 8); // $long-= 1 << ($nbytes * 8); } } return $long; }
public function testDivisionByZeroRaisesException() { $this->setExpectedException('Zend\\Math\\BigInteger\\Exception\\DivisionByZeroException', 'Division by zero'); $this->adapter->div('12345', '0'); }