From e96329ac0383dc5e9eaf9fdee9c6cdf36ba2ab8f Mon Sep 17 00:00:00 2001 From: Steven Tracey Date: Thu, 3 Nov 2022 12:47:44 -0400 Subject: [PATCH] Upload --- .idea/.gitignore | 8 +++ .idea/HexDecBinCalculator.iml | 8 +++ .idea/modules.xml | 8 +++ .idea/php.xml | 4 ++ conversions.php | 75 +++++++++++++++++++++++ index.php | 111 ++++++++++++++++++++++++++++++++++ style.css | 35 +++++++++++ 7 files changed, 249 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/HexDecBinCalculator.iml create mode 100644 .idea/modules.xml create mode 100644 .idea/php.xml create mode 100644 conversions.php create mode 100644 index.php create mode 100644 style.css diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -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 diff --git a/.idea/HexDecBinCalculator.iml b/.idea/HexDecBinCalculator.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/HexDecBinCalculator.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8e58e57 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/php.xml b/.idea/php.xml new file mode 100644 index 0000000..7e5d55a --- /dev/null +++ b/.idea/php.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/conversions.php b/conversions.php new file mode 100644 index 0000000..86b3b95 --- /dev/null +++ b/conversions.php @@ -0,0 +1,75 @@ + 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); +} \ No newline at end of file diff --git a/index.php b/index.php new file mode 100644 index 0000000..b87de5f --- /dev/null +++ b/index.php @@ -0,0 +1,111 @@ + + + + + + HexDecBin Calc + + +
+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+
+ + diff --git a/style.css b/style.css new file mode 100644 index 0000000..32c815c --- /dev/null +++ b/style.css @@ -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; +}