]> git.mxchange.org Git - core.git/blobdiff - framework/main/classes/utils/string/class_StringUtils.php
Continued:
[core.git] / framework / main / classes / utils / string / class_StringUtils.php
index 8d4a22807de179ad8ab92ad6fb09243f91735bef..d895d1911d15ab342cff8262bdde2ffa714e3009 100644 (file)
@@ -48,22 +48,66 @@ final class StringUtils extends BaseFrameworkSystem {
         * Array with bitmasks and such for pack/unpack methods to support both
         * 32-bit and 64-bit systems
         */
-       private static $packingData = array(
-               32 => array(
+       private static $packingData = [
+               32 => [
                        'step'   => 3,
                        'left'   => 0xffff0000,
                        'right'  => 0x0000ffff,
                        'factor' => 16,
                        'format' => 'II',
-               ),
-               64 => array(
+               ],
+               64 => [
                        'step'   => 7,
                        'left'   => 0xffffffff00000000,
                        'right'  => 0x00000000ffffffff,
                        'factor' => 32,
                        'format' => 'NN'
-               )
-       );
+               ]
+       ];
+
+       /**
+        * Hexadecimal->Decimal translation array
+        */
+       private static $hexdec = [
+               '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 = [
+                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'
+       ];
 
        /**
         * Simple 64-bit check, thanks to "Salman A" from stackoverflow.com:
@@ -191,6 +235,144 @@ final class StringUtils extends BaseFrameworkSystem {
                return $price;
        }
 
+       /**
+        * 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
+        */
+       public static function hex2dec (string $hex) {
+               // Convert to all lower-case
+               $hex = strtolower($hex);
+
+               // Detect sign (negative/positive numbers)
+               $sign = '';
+               if (substr($hex, 0, 1) == '-') {
+                       $sign = '-';
+                       $hex = substr($hex, 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 = self::$hexdec[substr($hex, $i, 1)];
+                       $dec = bcadd($dec, bcmul($factor, $e));
+               }
+
+               // Return the decimal number
+               return $sign . $dec;
+       }
+
+       /**
+        * Converts even very large decimal numbers, also signed, 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
+        * @param       $maxLength      Optional maximum length of the string
+        * @return      $hex    Hexadecimal string
+        */
+       public static function dec2hex (string $dec, int $maxLength = 0) {
+               // maxLength can be zero or devideable by 2
+               assert(($maxLength == 0) || (($maxLength % 2) == 0));
+
+               // Detect sign (negative/positive numbers)
+               $sign = '';
+               if ($dec < 0) {
+                       $sign = '-';
+                       $dec = abs($dec);
+               }
+
+               // Encode the decimal number into a hexadecimal string
+               $hex = '';
+               do {
+                       $hex = self::$dechex[($dec % (2 ^ 4))] . $hex;
+                       $dec /= (2 ^ 4);
+               } while ($dec >= 1);
+
+               /*
+                * Leading zeros are required for hex-decimal "numbers". In some
+                * situations more leading zeros are wanted, so check for both
+                * conditions.
+                */
+               if ($maxLength > 0) {
+                       // Prepend more zeros
+                       $hex = str_pad($hex, $maxLength, '0', STR_PAD_LEFT);
+               } elseif ((strlen($hex) % 2) != 0) {
+                       // Only make string's length dividable by 2
+                       $hex = '0' . $hex;
+               }
+
+               // 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 static function asc2dec (string $asc) {
+               // Convert it into a hexadecimal number
+               $hex = bin2hex($asc);
+
+               // And back into a decimal number
+               $dec = self::hex2dec($hex);
+
+               // Return it
+               return $dec;
+       }
+
+       /**
+        * Converts a decimal number into an ASCII string.
+        *
+        * @param       $dec            Decimal number
+        * @return      $asc    An ASCII string
+        */
+       public static function dec2asc (string $dec) {
+               // First convert the number into a hexadecimal string
+               $hex = self::dec2hex($dec);
+
+               // Then convert it into the ASCII string
+               $asc = self::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 static function hex2asc ($hex) {
+               // Check for length, it must be devideable by 2
+               //* DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('hex='.$hex);
+               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));
+
+                       // Add it to the final string
+                       $asc .= chr($part);
+               }
+
+               // Return the final string
+               return $asc;
+       }
+
        /**
         * Pack a string into a "binary format". Please execuse me that this is
         * widely undocumented. :-(