]> git.mxchange.org Git - core.git/commitdiff
Added methods dec2hex()/hex2dec() which are based on user comments at php.net documen...
authorRoland Häder <roland@mxchange.org>
Mon, 4 Apr 2011 22:19:12 +0000 (22:19 +0000)
committerRoland Häder <roland@mxchange.org>
Mon, 4 Apr 2011 22:19:12 +0000 (22:19 +0000)
inc/classes/main/class_BaseFrameworkSystem.php

index d092ccbf78e1c0842a00d6c0028a220c6d0d320f..7f871069e191b3491441120a24c30248fa0c258d 100644 (file)
@@ -1551,6 +1551,107 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface {
        public function getPackageData () {
                return $this->packageData;
        }
+
+       /**
+        * Converts a hexadecimal string, even with negative sign as first string to
+        * a decimal number using BC functions.
+        *
+        * This work is based on comment #86673 on php.net documentation page at:
+        * <http://de.php.net/manual/en/function.dechex.php#86673>
+        *
+        * @param       $hex    Hexadecimal string
+        * @return      $dec    Decimal number
+        */
+       function hex2dec ($hex) {
+               // Convert to all lower-case
+               $hex = strtolower($hex);
+
+               // Hexadecimal->Decimal translation array
+               $hexdec = 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
+               );
+
+               // Detect sign (negative/positive numbers)
+               $sign = '';
+               if (substr($hex, 0, 1) == '-') {
+                       $sign = '-';
+                       $hex = substr($hex, 1);
+               } // END - if
+
+               // Decode the hexadecimal string into a decimal number
+               $dec = 0;
+               for ($i = strlen($hex) - 1, $e = 1; $i >= 0; $i--, $e = bcmul($e, 16)) {
+                       $factor = $hexdec[substr($hex, $i, 1)];
+                       $dec = bcadd($dec, bcmul($factor, $e));
+               } // END - for
+
+               // Return the decimal number
+               return $sign . $dec;
+       }
+
+       /**
+        * Converts even very large decimal numbers, also with negative sign, to a
+        * hexadecimal string.
+        *
+        * This work is based on comment #97756 on php.net documentation page at:
+        * <http://de.php.net/manual/en/function.hexdec.php#97756>
+        *
+        * @param       $dec    Decimal number, even with negative sign
+        * @return      $hex    Hexadecimal string
+        */
+       function dec2hex ($dec) {
+               // Detect sign (negative/positive numbers)
+               $sign = '';
+               if ($dec < 0) {
+                       $sign = '-';
+                       $dec = abs($dec);
+               } // END - if
+
+               // Decimal->hexadecimal translation array
+               $hexAlphabet = 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'
+               );
+
+               // Encode the decimal number into a hexadecimal string
+               $hex = '';
+               do {
+                       $hex = $hexAlphabet[($dec % 16)] . $hex;
+                       $dec /= 16;
+               } while( $dec >= 1 );
+
+               // Return the hexadecimal string
+               return $sign . $hex;
+       }
 }
 
 // [EOF]