This commit is contained in:
Steven Tracey 2022-11-03 12:47:44 -04:00
parent 3c9c8e200f
commit e96329ac03
7 changed files with 249 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/HexDecBinCalculator.iml" filepath="$PROJECT_DIR$/.idea/HexDecBinCalculator.iml" />
</modules>
</component>
</project>

4
.idea/php.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpProjectSharedConfiguration" php_language_level="8.1" />
</project>

75
conversions.php Normal file
View File

@ -0,0 +1,75 @@
<?php
function hex_to_decimal(string $hex) : int {
$chars = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8,
9 => 9, "A" => 10, "B" => 11, "C" => 12, "D" => 13, "E" => 14, "F" => 15);
$decimal = 0;
$hex_chars = str_split(strrev(strtoupper($hex)));
for ($i = 0; $i < sizeof($hex_chars); $i++) {
$decimal += $chars[$hex_chars[$i]] * (16 ** $i);
}
return $decimal;
}
function hex_to_binary(string $hex) : int {
$decimal = hex_to_decimal($hex);
return decimal_to_binary($decimal);
}
function decimal_to_hex(int $decimal) : string {
$chars = array(0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8,
9 => 9, 10 => "A", 11 => "B", 12 => "C", 13 => "D", 14 => "E", 15 => "F");
$remainder = $decimal;
$hex_string = "";
while ($remainder > 0) {
if ($remainder < 16) {
$hex_string = $chars[$remainder] . $hex_string;
break;
} else {
$temp_remainder = $remainder % 16;
$quotient = ($remainder - $temp_remainder) / 16;
$hex_string = $chars[$temp_remainder] . $hex_string;
$remainder = $quotient;
}
}
return $hex_string;
}
function decimal_to_binary(int $decimal) : int {
$remainder = $decimal;
$greatest_base_2 = 0;
$binary_str = "";
for ($i = 0; (2 ** $i) < $remainder; $i++) {
$greatest_base_2 = $i;
}
while ($greatest_base_2 >= 0) {
if ($remainder < (2 ** $greatest_base_2)) {
$binary_str = $binary_str . "0";
} else {
$remainder = $remainder - (2 ** $greatest_base_2);
$binary_str = $binary_str . "1";
}
$greatest_base_2--;
}
return intval($binary_str);
}
function binary_to_decimal(int $binary) : int {
$digits = str_split(strrev(strval($binary)));
$decimal = 0;
for ($i = 0; $i < sizeof($digits); $i++) {
if ($digits[$i] == "1") {
$decimal = $decimal + (2 ** $i);
}
}
return $decimal;
}
function binary_to_hex(int $binary) : string {
$decimal = binary_to_decimal($binary);
return decimal_to_hex($decimal);
}

111
index.php Normal file
View File

@ -0,0 +1,111 @@
<?php
include "./conversions.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="./style.css">
<title>HexDecBin Calc</title>
</head>
<body>
<div class="inputs">
<form action="index.php" method="get">
<?php
session_start();
$bin = 0;
$dec = 0;
$hex = 0;
$new = false;
if (!isset($_GET['binary']) ||
!isset($_GET['decimal']) ||
!isset($_GET['hexadecimal'])) {
$new = true;
}
if (!isset($_SESSION['bin']) ||
!isset($_SESSION['dec']) ||
!isset($_SESSION['hex'])) {
$_SESSION['bin'] = 0;
$_SESSION['dec'] = 0;
$_SESSION['hex'] = 0;
}
if (!$new) {
$last_bin = $_SESSION['bin'];
$last_dec = $_SESSION['dec'];
$last_hex = $_SESSION['hex'];
$bin = $_GET['binary'];
$dec = $_GET['decimal'];
$hex = $_GET['hexadecimal'];
if ($bin == 0 && $dec == 0) {
$bin = hex_to_binary($hex);
$_SESSION['bin'] = $bin;
$dec = hex_to_decimal($hex);
$_SESSION['dec'] = $dec;
$_SESSION['hex'] = $hex;
} else if ($bin == 0 && $hex == 0) {
$bin = decimal_to_binary($dec);
$_SESSION['bin'] = $bin;
$_SESSION['dec'] = $dec;
$hex = decimal_to_hex($dec);
$_SESSION['hex'] = $hex;
} else if ($dec == 0 && $hex == 0) {
$_SESSION['bin'] = $bin;
$dec = binary_to_decimal($bin);
$_SESSION['dec'] = $dec;
$hex = binary_to_hex($bin);
$_SESSION['hex'] = $hex;
}
else {
if ($bin == $last_bin && $dec == $last_dec && $hex != $last_hex) {
$bin = hex_to_binary($hex);
$_SESSION['bin'] = $bin;
$dec = hex_to_decimal($hex);
$_SESSION['dec'] = $dec;
$_SESSION['hex'] = $hex;
} else if ($bin == $last_bin && $hex == $last_hex && $dec != $last_dec) {
$bin = decimal_to_binary($dec);
$_SESSION['bin'] = $bin;
$_SESSION['dec'] = $dec;
$hex = decimal_to_hex($dec);
$_SESSION['hex'] = $hex;
} else if ($dec == $last_dec && $hex == $last_hex && $bin != $last_bin) {
$_SESSION['bin'] = $bin;
$dec = binary_to_decimal($bin);
$_SESSION['dec'] = $dec;
$hex = binary_to_hex($bin);
$_SESSION['hex'] = $hex;
} else {
$bin = 0;
$dec = 0;
$hex = 0;
}
}
}
?>
<div>
<label for="binary">Binary: </label>
<input type="text" id="binary" name="binary" value="<?php echo $bin; ?>">
</div>
<div>
<label for="decimal">Decimal: </label>
<input type="text" id="decimal" name="decimal" value="<?php echo $dec; ?>">
</div>
<div>
<label for="hexadecimal">Hexadecimal: </label>
<input type="text" id="hexadecimal" name="hexadecimal" value="<?php echo $hex; ?>">
</div>
<div>
<input class="submit" type="submit">
</div>
</form>
</div>
</body>
</html>

35
style.css Normal file
View File

@ -0,0 +1,35 @@
* {
background-color: #27263D;
color: white;
font-family: "Trebuchet MS", serif;
}
.inputs {
margin: 50px auto auto;
width: 50%;
border: 2px solid black;
border-radius: 50px;
padding: 10px;
}
.submit {
margin: auto 30%;
width: 40%;
padding: 10px;
}
div {
margin-bottom: 10px;
}
label {
display: inline-block;
width: 40%;
text-align: right;
}
input {
margin: 10px auto auto;
width: 25%;
padding: 10px;
}