mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 07:08:13 +01:00
Added Binary, Octal, and Hex conversion to and from Decimal Numbers
This commit is contained in:
parent
1b86b577a9
commit
28deb2c0e6
53
Conversions/BinaryToDecimal.php
Normal file
53
Conversions/BinaryToDecimal.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* This function converts the
|
||||
* submitted Binary Number to
|
||||
* Decimal Number.
|
||||
*
|
||||
* Working of Algorithm
|
||||
* (10) base 2
|
||||
* (1 * (2 ^ 1) + 0 * (2 ^ 0)) base 10
|
||||
* (2 + 0) base 10
|
||||
* 2 base 10
|
||||
* @param string $binaryNumber
|
||||
* @return int
|
||||
*/
|
||||
function binaryToDecimal($binaryNumber)
|
||||
{
|
||||
if (!is_numeric($binaryNumber)) {
|
||||
throw new \Exception('Please pass a valid Binary Number for Converting it to a Decimal Number.');
|
||||
}
|
||||
|
||||
$decimalNumber = 0;
|
||||
$binaryDigits = array_reverse(str_split($binaryNumber));
|
||||
|
||||
foreach ($binaryDigits as $index => $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;
|
||||
}
|
93
Conversions/HexadecimalToDecimal.php
Normal file
93
Conversions/HexadecimalToDecimal.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* This function converts the
|
||||
* submitted Octal Number to
|
||||
* Decimal Number.
|
||||
*
|
||||
* Working of Algorithm
|
||||
* (AB) base 16
|
||||
* (A * (16 ^ 1) + B * (16 ^ 0)) base 10
|
||||
* (10 * (16 ^ 1) + 11 * (16 ^ 0)) base 10
|
||||
* (160 + 11) base 10
|
||||
* 171 base 10
|
||||
* @param string $octalNumber
|
||||
* @return int
|
||||
*/
|
||||
function hexToDecimal($hexNumber)
|
||||
{
|
||||
// Using ctype to check all the digits are valid hex digits or not.
|
||||
if (!ctype_xdigit($hexNumber)) {
|
||||
throw new \Exception('Please pass a valid Hexadecimal Number for Converting it to a Decimal Number.');
|
||||
}
|
||||
|
||||
$decimalNumber = 0;
|
||||
|
||||
// Mapping for Decimal Digits after 9
|
||||
$decimalDigitMappings = [
|
||||
'A' => 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;
|
||||
}
|
53
Conversions/OctalToDecimal.php
Normal file
53
Conversions/OctalToDecimal.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* This function converts the
|
||||
* submitted Octal Number to
|
||||
* Decimal Number.
|
||||
*
|
||||
* Working of Algorithm
|
||||
* (10) base 8
|
||||
* (1 * (8 ^ 1) + 0 * (8 ^ 0)) base 10
|
||||
* (8 + 0) base 10
|
||||
* 9 base 10
|
||||
* @param string $octalNumber
|
||||
* @return int
|
||||
*/
|
||||
function octalToDecimal($octalNumber)
|
||||
{
|
||||
if (!is_numeric($octalNumber)) {
|
||||
throw new \Exception('Please pass a valid Octal Number for Converting it to a Decimal Number.');
|
||||
}
|
||||
|
||||
$decimalNumber = 0;
|
||||
$octalDigits = array_reverse(str_split($octalNumber));
|
||||
|
||||
foreach ($octalDigits as $index => $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;
|
||||
}
|
71
tests/ConversionsTest.php
Normal file
71
tests/ConversionsTest.php
Normal file
@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
use function PHPUnit\Framework\assertEquals;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
require_once __DIR__ . '/../Conversions/BinaryToDecimal.php';
|
||||
require_once __DIR__ . '/../Conversions/OctalToDecimal.php';
|
||||
require_once __DIR__ . '/../Conversions/HexadecimalToDecimal.php';
|
||||
|
||||
class ConversionsTest extends TestCase
|
||||
{
|
||||
public function testBinaryToDecimal()
|
||||
{
|
||||
assertEquals(binaryToDecimal(111), 7);
|
||||
assertEquals(binaryToDecimal(101), 5);
|
||||
$this->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");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user