From acb9e8776771abf9789ef9b7dd8dc92ed8c9fdd8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 4 Apr 2011 23:07:22 +0000 Subject: [PATCH] Moved translation arrays, added new convertion functions: - Moved translation arrays from previously added convertion functions as static, private attributes to the top - Added two new functions dec2asc() and asc2dec() --- .../main/class_BaseFrameworkSystem.php | 150 ++++++++++++------ 1 file changed, 105 insertions(+), 45 deletions(-) diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 7f871069..db8d9776 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -225,6 +225,46 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { const EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED = 0x039; const EXCEPTION_FILTER_CHAIN_INTERCEPTED = 0x040; + // Hexadecimal->Decimal translation array + private static $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 + ); + + // Decimal->hexadecimal translation array + private static $dechex = 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' + ); + /** * Protected super constructor * @@ -1562,30 +1602,10 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * @param $hex Hexadecimal string * @return $dec Decimal number */ - function hex2dec ($hex) { + public final 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) == '-') { @@ -1596,7 +1616,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { // 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)]; + $factor = self:$hexdec[substr($hex, $i, 1)]; $dec = bcadd($dec, bcmul($factor, $e)); } // END - for @@ -1614,7 +1634,7 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { * @param $dec Decimal number, even with negative sign * @return $hex Hexadecimal string */ - function dec2hex ($dec) { + public final function dec2hex ($dec) { // Detect sign (negative/positive numbers) $sign = ''; if ($dec < 0) { @@ -1622,36 +1642,76 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { $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; + $hex = self:$dechex[($dec % 16)] . $hex; $dec /= 16; - } while( $dec >= 1 ); + } while ($dec >= 1); // Return the hexadecimal string return $sign . $hex; } + + /** + * Converts a ASCII string (0 to 255) into a decimal number. + * + * @param $asc The ASCII string to be converted + * @return $dec Decimal number + */ + public final function asc2dec ($asc) { + // Convert it into a hexadecimal number + $hex = bin2hex($asc); + + // And back into a decimal number + $dec = $this->hex2dec($hex); + + // Return it + return $dec; + } + + /** + * Converts a decimal number into an ASCII string. + * + * @param $dec Decimal number + * @return $asc An ASCII string + */ + public final function dec2asc ($deg) { + // First convert the number into a hexadecimal string + $hex = $this->dec2hex($deg); + + // Then convert it into the ASCII string + $asc = $this->hex2asc($hex); + + // Return it + return $asc; + } + + /** + * Converts a hexadecimal number into an ASCII string. Negative numbers + * are not allowed. + * + * @param $hex Hexadecimal string + * @return $asc An ASCII string + */ + public final function hex2asc ($hex) { + // Check for length, it must be devideable by 2 + assert((strlen($hex) % 2) == 0); + + // Walk the string + $asc = ''; + for ($idx = 0; $idx < strlen($hex); $idx+=2) { + // Get the decimal number of the chunk + $part = hexdec(substr($hex, $idx, 2)); + $this->debugOutput(__FUNCTION__ . ': part(' . strlen($part) . ')=' . $part); + + // Add it to the final string + $asc .= chr($part); + } // END - for + + // Return the final string + return $asc; + } } // [EOF] -- 2.30.2