author Roland Häder Mon, 4 Apr 2011 22:19:12 +0000 (22:19 +0000) committer Roland Häder Mon, 4 Apr 2011 22:19:12 +0000 (22:19 +0000)

index d092ccb..7f87106 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);
+
+               \$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
+        *
+        * 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
+
+               \$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]