/** * 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)); }