PHP-如何base_convert()最多达到62 [英] PHP - How to base_convert() up to base 62

查看:89
本文介绍了PHP-如何base_convert()最多达到62的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个从c2到62的c0>函数,但是我缺少需要使用的数学运算,我知道由于PHP的限制,我需要使用bcmath,这是很好.

I need a base_convert() function that works from base 2 up to base 62 but I'm missing the math I need to use, I know that due to the limitations of PHP I need to make use of bcmath, which is fine.

函数像这样将数字转换为以10为底的数字另一个基数最多为62,但是我想实现base_convert()的相同功能,例如:唯一一个可以在任意基数之间转换的函数.

Functions like these convert a number to and from base 10 to another base up to 62, but I want to implement the same functionality of base_convert(), e.g.: a only one function that can convert between arbitrary bases.

我发现了一个功能似乎可以做到的,但这给了我一些冗余和慢速代码的感觉,如果我知道德语,我想对其进行一些调整,但我不知道. =(

I've found a function that seems to do this, but it gives me the feeling of having some redundant and slow code and I would like to tweak it a little bit if I knew German, which I don't. =(

以下是该函数的可读性更高的版本:

Here is a more readable version of the function:

function bc_base_convert($value, $quellformat, $zielformat)
{
    $vorrat = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

    if (min($quellformat, $zielformat) < 2)
    {
        trigger_error('Bad Format min: 2', E_USER_ERROR);
    }

    if (max($quellformat, $zielformat) > strlen($vorrat))
    {
        trigger_error('Bad Format max: ' . strlen($vorrat), E_USER_ERROR);
    }

    $dezi = '0';
    $level = 0;
    $result = '';
    $value = trim(strval($value), "\r\n\t +");
    $vorzeichen = '-' === $value{0} ? '-' : '';
    $value = ltrim($value, "-0");
    $len = strlen($value);

    for ($i = 0; $i < $len; $i++)
    {
        $wert = strpos($vorrat, $value{$len - 1 - $i});

        if (FALSE === $wert)
        {
            trigger_error('Bad Char in input 1', E_USER_ERROR);
        }

        if ($wert >= $quellformat)
        {
            trigger_error('Bad Char in input 2', E_USER_ERROR);
        }

        $dezi = bcadd($dezi, bcmul(bcpow($quellformat, $i), $wert));
    }

    if (10 == $zielformat)
    {
        return $vorzeichen . $dezi; // abkürzung
    }

    while (1 !== bccomp(bcpow($zielformat, $level++), $dezi));

    for ($i = $level - 2; $i >= 0; $i--)
    {
        $factor = bcpow($zielformat, $i);
        $zahl = bcdiv($dezi, $factor, 0);
        $dezi = bcmod($dezi, $factor);
        $result .= $vorrat{$zahl};
    }

    $result = empty($result) ? '0' : $result;

    return $vorzeichen . $result;
}

任何人都可以向我解释上述功能,或者给我一些关于任意基数之间直接转换的过程的提示吗?

Can anyone explain me the above function or give me some lights on the process of direct conversion between arbitrary bases?

推荐答案

从PHP 5.3.2开始,bc_math和gmp现在都支持最多62个基数,因此您可以这样做:

As of PHP 5.3.2 both bc_math and gmp now support bases up to 62, so you can just do:

echo gmp_strval(gmp_init($mynumber, $srcbase), $destbase);

或等效的bc_math.

or the bc_math equivalent.

这篇关于PHP-如何base_convert()最多达到62的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆