}
/**
- * Converts even very large decimal numbers, also with negative sign, to a
- * hexadecimal string.
+ * 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>
// Encode the decimal number into a hexadecimal string
$hex = '';
do {
- $hex = self::$dechex[($dec % 16)] . $hex;
- $dec /= 16;
+ $hex = self::$dechex[($dec % (2 ^ 4))] . $hex;
+ $dec /= (2 ^ 4);
} while ($dec >= 1);
/*
- * We need hexadecimal strings with leading zeros if the length cannot
- * be divided by 2
+ * 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 = $this->prependStringToString($hex, '0', $maxLength);
+ $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 $asc;
}
- /**
- * Prepends a given string $prepend to $str with a given total length
- *
- * @param $str Given original string which should be prepended
- * @param $prepend The string to prepend
- * @param $length Total length of the final string
- * @return $strFinal Final prepended string
- */
- protected function prependStringToString ($str, $prepend, $length) {
- // Set final string to original string by default
- $strFinal = $str;
-
- // Can it devided
- if (strlen($str) < $length) {
- // Difference between total length and length of original string
- $diff = $length - strlen($str);
-
- // Prepend the string
- $prepend = str_repeat($prepend, ($diff / strlen($prepend) + 1));
-
- // Make sure it will definedly fit
- assert(strlen($prepend) >= $diff);
-
- // Cut it a little down
- $prepend = substr($prepend, 0, $diff);
- //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('prepend('.strlen($prepend).')='.$prepend.',diff='.$diff.',length='.$length);
-
- // Construct the final prepended string
- $strFinal = $prepend . $str;
- } // END - if
-
- // Return it
- return $strFinal;
- }
-
/**
* Checks whether the given encoded data was encoded with Base64
*