diff --git a/Conversions/BinaryToDecimal.php b/Conversions/BinaryToDecimal.php new file mode 100644 index 0000000..b80ab36 --- /dev/null +++ b/Conversions/BinaryToDecimal.php @@ -0,0 +1,53 @@ + $digit) { + $decimalNumber += $digit * pow(2, $index); + } + + return $decimalNumber; +} + +/** + * This function converts the + * submitted Decimal Number to + * Binary Number. + * + * @param string $decimalNumber + * @return string + */ +function decimalToBinary($decimalNumber) +{ + if (!is_numeric($decimalNumber)) { + throw new \Exception('Please pass a valid Decimal Number for Converting it to a Binary Number.'); + } + + $binaryNumber = ''; + + while ($decimalNumber > 0) { + $binaryNumber = ($decimalNumber % 2) . $binaryNumber; + $decimalNumber /= 2; + } + + return $binaryNumber; +} diff --git a/Conversions/HexadecimalToDecimal.php b/Conversions/HexadecimalToDecimal.php new file mode 100644 index 0000000..461506c --- /dev/null +++ b/Conversions/HexadecimalToDecimal.php @@ -0,0 +1,93 @@ + 10, + 'B' => 11, + 'C' => 12, + 'D' => 13, + 'E' => 14, + 'F' => 15, + ]; + + $hexDigits = str_split($hexNumber); + $hexDigits = array_reverse($hexDigits); + + foreach ($hexDigits as $power => $digit) { + $hexDigit = $digit; + if (!is_numeric($digit)) { + $hexDigit = $decimalDigitMappings[$digit]; + } + $decimalNumber += (pow(16, $power) * $hexDigit); + } + return $decimalNumber; +} + +/** + * This function converts the + * submitted Decimal Number to + * Hexadecimal Number. + * + * @param string $decimalNumber + * @return string + */ +function decimalToHex($decimalNumber) +{ + $hexDigits = []; + + // Mapping for HexaDecimal Digits after 9 + $hexDigitMappings = [ + 10 => 'A', + 11 => 'B', + 12 => 'C', + 13 => 'D', + 14 => 'E', + 15 => 'F', + ]; + if (!is_numeric($decimalNumber)) { + throw new \Exception('Please pass a valid Decimal Number for Converting it to a Hexadecimal Number.'); + } + + while ($decimalNumber > 0) { + $remainder = ($decimalNumber % 16); + $decimalNumber /= 16; + if (empty($hexDigits) && 0 === $remainder) { + continue; + } + $hexDigits[] = $remainder; + } + + $hexDigits = array_reverse($hexDigits); + + foreach ($hexDigits as $index => $digit) { + if ($digit > 9) { + $hexDigits[$index] = $hexDigitMappings[$digit]; + } + } + + $hexNumber = ltrim(implode('', $hexDigits), '0'); // Connecting all the digits and removing leading zeroes. + + return $hexNumber; +} diff --git a/Conversions/OctalToDecimal.php b/Conversions/OctalToDecimal.php new file mode 100644 index 0000000..cde3638 --- /dev/null +++ b/Conversions/OctalToDecimal.php @@ -0,0 +1,53 @@ + $digit) { + $decimalNumber += $digit * pow(8, $index); + } + + return $decimalNumber; +} + +/** + * This function converts the + * submitted Decimal Number to + * Octal Number. + * + * @param string $decimalNumber + * @return string + */ +function decimalToOctal($decimalNumber) +{ + if (!is_numeric($decimalNumber)) { + throw new \Exception('Please pass a valid Decimal Number for Converting it to an Octal Number.'); + } + + $octalNumber = ''; + + while ($decimalNumber > 0) { + $octalNumber = ($decimalNumber % 8) . $octalNumber; + $decimalNumber /= 8; + } + + return $octalNumber; +} diff --git a/tests/ConversionsTest.php b/tests/ConversionsTest.php new file mode 100644 index 0000000..3aa41bc --- /dev/null +++ b/tests/ConversionsTest.php @@ -0,0 +1,71 @@ +expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Binary Number for Converting it to a Decimal Number.'); + binaryToDecimal("this is a string"); + } + + public function testDecimalToBinary() + { + assertEquals(decimalToBinary(7), 111); + assertEquals(decimalToBinary(5), 101); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to a Binary Number.'); + decimalToBinary("this is a string"); + } + + public function testOctalToDecimal() + { + assertEquals(octalToDecimal(10), 8); + assertEquals(octalToDecimal(11), 9); + assertEquals(octalToDecimal(1115), 589); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Octal Number for Converting it to a Decimal Number.'); + octalToDecimal("this is a string"); + } + + public function testDecimalToOctal() + { + assertEquals(decimalToOctal(8), 10); + assertEquals(decimalToOctal(9), 11); + assertEquals(decimalToOctal(589), 1115); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to an Octal Number.'); + decimalToOctal("this is a string"); + } + + public function testDecimalToHex() + { + assertEquals(decimalToHex(10), 'A'); + assertEquals(decimalToHex(489201875), '1D28A0D3'); + assertEquals(decimalToHex(171), 'AB'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Decimal Number for Converting it to a Hexadecimal Number.'); + decimalToHex("this is a string"); + } + + public function testHexToDecimal() + { + assertEquals(hexToDecimal('A'), 10); + assertEquals(hexToDecimal('1D28A0D3'), 489201875); + assertEquals(hexToDecimal('AB'), 171); + $this->expectException(\Exception::class); + $this->expectExceptionMessage('Please pass a valid Hexadecimal Number for Converting it to a Decimal Number.'); + hexToDecimal("this is a string"); + } +}