Convert a “big” Hex number (string format) to a de

2020-02-08 08:37发布

How to convert a "big" Hex number (in string format):

EC851A69B8ACD843164E10CFF70CF9E86DC2FEE3CF6F374B43C854E3342A2F1AC3E30C741CC41E679DF6D07CE6FA3A66083EC9B8C8BF3AF05D8BDBB0AA6CB3EF8C5BAA2A5E531BA9E28592F99E0FE4F95169A6C63F635D0197E325C5EC76219B907E4EBDCD401FB1986E4E3CA661FF73E7E2B8FD9988E753B7042B2BBCA76679

to a decimal number (in string format):

166089946137986168535368849184301740204613753693156360462575217560130904921953976324839782808018277000296027060873747803291797869684516494894741699267674246881622658654267131250470956587908385447044319923040838072975636163137212887824248575510341104029461758594855159174329892125993844566497176102668262139513

without using BigInteger Class (as my application should support machines without .NET Framework 4)?

7条回答
孤傲高冷的网名
2楼-- · 2020-02-08 09:18

I just translated Douglas code to PHP:

function BigNumberHexToDecimal($hex)
{
    $dec = array(0);
    $hexLen = strlen($hex);
    for($h=0;$h<$hexLen;++$h)
    {
        $carry = hexdec($hex[$h]);
        for ($i = 0; $i < count($dec); ++$i)
        {
            $val = $dec[$i] * 16 + $carry;
            $dec[$i] = $val % 10;
            $carry = (int)($val / 10);
        }
        while ($carry > 0)
        {
            $dec[] = $carry % 10;
            $carry = (int)($carry / 10);
        }
    }

    return join("", array_reverse($dec));
}
查看更多
登录 后发表回答