/** * 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; } }
/** * @param string $op * @param string $bin * @param string $bin2c * @dataProvider binaryConversionProvider */ public function testBinaryConversion($op, $bin, $bin2c) { $bin = base64_decode($bin); $bin2c = base64_decode($bin2c); $opPos = ltrim($op, '-'); $this->assertEquals($bin, $this->adapter->intToBin($op)); $this->assertEquals($bin2c, $this->adapter->intToBin($op, true)); $this->assertEquals($opPos, $this->adapter->binToInt($bin)); $this->assertEquals($op, $this->adapter->binToInt($bin2c, true)); }
/** * 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; }