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
*
* @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) == '-') {
// 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
* @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) {
$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]