Skip to content

Commit

Permalink
HexDec method update
Browse files Browse the repository at this point in the history
  • Loading branch information
furqansiddiqui committed May 29, 2018
1 parent 38b24b7 commit 42c57b7
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/BcMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 42c57b7

Please sign in to comment.