Algorithm-how does php convert a 20-bit 62-hexadecimal string back to a 10-hexadecimal string?

Source: Internet
Author: User
Tags string back
Php converts a 20-bit 62-digit string to a 10-digit string. At present, {code...} can be used to convert a super-long decimal integer to the 62 hexadecimal notation. However, due to system restrictions, it will become a number such as 9.9999999991447E + 27. An algorithm is required to convert the super-long 62 hexadecimal into... php to convert a 20-bit 62 hexadecimal string back to the 10 hexadecimal string.
Currently

    function dec62($n) {        $base = 62;        $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';        $ret = '';        for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) {            $a = floor($n / pow($base, $t));            $ret .= substr($index, $a, 1);            $n -= $a * pow($base, $t);        }        return $ret;    }

The super-long decimal integer can be converted to the 62-digit system. However, due to system restrictions, it will become9.9999999991447E+27Such a number.
An algorithm is required to convert the super-long 62 hexadecimal string back to the 10 hexadecimal string.
You can use the following number to test
9999999999144705880199999999999

Reply content:

Php converts a 20-bit 62-digit string to a 10-digit string.
Currently

    function dec62($n) {        $base = 62;        $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';        $ret = '';        for($t = floor(log10($n) / log10($base)); $t >= 0; $t --) {            $a = floor($n / pow($base, $t));            $ret .= substr($index, $a, 1);            $n -= $a * pow($base, $t);        }        return $ret;    }

The super-long decimal integer can be converted to the 62-digit system. However, due to system restrictions, it will become9.9999999991447E+27Such a number.
An algorithm is required to convert the super-long 62 hexadecimal string back to the 10 hexadecimal string.
You can use the following number to test
9999999999144705880199999999999

Use BCMath for addition, subtraction, multiplication, and division of any precision.

function base62to10($n){    $index = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';    $n = strval($n);    $len = strlen($n);    $result = 0;    $base = 1;    for ($i = $len-1; $i >= 0; $i--) {        $char = $n[$i];        $d = strpos($index, $char);        assert($d !== false);        $result = bcadd($result, bcmul($d, $base));        $base = bcmul($base, 62);    }    return $result;}echo base62to10('9999999999144705880199999999999'),"\n";

First, the author's algorithm is incorrect, Because I tested dec62 () using the numbers 9999999999144705880199999999999 and 9999999999144705880199999999998 and found that the output results were the same.
I saw the great algorithm in the php Manual (BCMath support is required ):

    function convBase($numberInput, $fromBaseInput, $toBaseInput){        if ($fromBaseInput==$toBaseInput) return $numberInput;        $fromBase = str_split($fromBaseInput,1);        $toBase = str_split($toBaseInput,1);        $number = str_split($numberInput,1);        $fromLen=strlen($fromBaseInput);        $toLen=strlen($toBaseInput);        $numberLen=strlen($numberInput);        $retval='';        if ($toBaseInput == '0123456789')        {            $retval=0;            for ($i = 1;$i <= $numberLen; $i++)                $retval = bcadd($retval, bcmul(array_search($number[$i-1], $fromBase),bcpow($fromLen,$numberLen-$i)));            return $retval;        }        if ($fromBaseInput != '0123456789')            $base10=convBase($numberInput, $fromBaseInput, '0123456789');        else            $base10 = $numberInput;        if ($base10
  

Usage:
1. Convert decimal to 62 hexadecimal

echo convBase('9999999999144705880199999999999','0123456789','0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');

2. Convert 62 to decimal

echo convBase('3nLqycbr6ZQsN1JJYX','0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','0123456789');

It supports any form of hexadecimal conversion, see the http://php.net/manual/en/function.base-convert.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.