From 42c57b72a8809d48ed974840060c8339bf884bbd Mon Sep 17 00:00:00 2001 From: "M. Furqan Siddiqui" Date: Tue, 29 May 2018 23:28:54 +0500 Subject: [PATCH] HexDec method update --- src/BcMath.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/BcMath.php b/src/BcMath.php index ab4f964..155027c 100644 --- a/src/BcMath.php +++ b/src/BcMath.php @@ -21,18 +21,17 @@ class BcMath { /** - * @param $hex - * @return int|float|string + * @param string $hex + * @return string */ public static function HexDec(string $hex): string { - if (strlen($hex) == 1) { - return strval(hexdec($hex)); - } else { - $remain = substr($hex, 0, -1); - $last = substr($hex, -1); - return bcadd(bcmul(16, self::HexDec($remain)), hexdec($last)); + $dec = 0; + $len = strlen($hex); + for ($i = 1; $i <= $len; $i++) { + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i)))); } + return $dec; } /**