请解释一下这个 PHP 62 进制转换函数/算法

发布于 2024-10-12 08:57:55 字数 594 浏览 2 评论 0原文

谁能解释一下下面的代码吗?或者向我指出一些资源,让我有所了解:)

它将整数转换为 base62 字符串。

private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

private static function _convertBase($num)
{
    $base = strlen(self::$_characters);
    $string = '';

    for ($t = floor(log10($num) / log10($base)); $t >= 0; $t--) {
        $a = floor($num / pow($base, $t));
        $string .= substr(self::$_characters, $a, 1);
        $num = $num - ($a * pow($base, $t));
    }

    return $string;
}

更新:我想问的是什么:有人可以解释一下下面的算法吗? :) 谢谢。

Could anyone please explain the code below? That or point me to some resources shedding some light :)

It converts an integer to a base62 string.

private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

private static function _convertBase($num)
{
    $base = strlen(self::$_characters);
    $string = '';

    for ($t = floor(log10($num) / log10($base)); $t >= 0; $t--) {
        $a = floor($num / pow($base, $t));
        $string .= substr(self::$_characters, $a, 1);
        $num = $num - ($a * pow($base, $t));
    }

    return $string;
}

Update: What I meant to ask: Could anyone please explain the algorithm below? :) Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌生 2024-10-19 08:57:55

你太复杂了:

private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

private static function _convertBase($num)
{
    $base = strlen(self::$_characters); // 62
    $string = self::$_characters[$num % $base];

    while (($num = intval($num / $base)) > 0)
    {
        $string = self::$_characters[$num % $base] . $string;
    }

    return $string;
}

You're over-complicating:

private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

private static function _convertBase($num)
{
    $base = strlen(self::$_characters); // 62
    $string = self::$_characters[$num % $base];

    while (($num = intval($num / $base)) > 0)
    {
        $string = self::$_characters[$num % $base] . $string;
    }

    return $string;
}
韬韬不绝 2024-10-19 08:57:55

使用对数时有一个公式:

logN(x) = log10(x) / log10(N)。

换句话说,以 N 为底的数字的对数等于该数字的对数(以 10 为底)除以底数的对数(同样以 10 为底)。

因此,您可以简单地使用本机 log10() 函数并相应地缩放数字,而不是为每个基数(例如基数 62)创建对数函数。

在这个特定的算法中,您想要确定要转换的数字以 62 为基数有多少位,因此您可以在“for”循环中使用它。

当然,您可以使用 while 循环来执行此操作,而无需计算 log62(n)。这是给读者的练习。

There is a formula when working with logarithms:

logN(x) = log10(x) / log10(N).

In other words, the log of a number in base N is equal to the log (in base 10) of the number divided by the log (again in base 10) of the base.

So instead of creating a logarithm function for each base, like base 62, you can simply use the native log10() function and scale the numbers accordingly.

And in this particular algorithm, you want to determine how many digits there are, in base 62, for the number you are converting, so you can use that in the "for" loop.

You could, of course, do this is with a while loop without having to calculate log62(n). This is an exercise for the reader.

清晨说晚安 2024-10-19 08:57:55

一个更加伪代码的版本。

// Maps some characters such that
//  0   ->'0'
//  9   ->'9'
//  10  ->'a'
//  35  ->'z'
//  36  ->'A'
//  61  ->'Z'
Let constant characters = List ('0'..'9', 'a'..'z', 'A'..'Z')
Let constant size = length of characters

Function LogBase(number base, number x)
    Return LogBase10(x) / LogBase10(base)

Function LeftMostPosition(unsigned integer message)
    Return Floor(LogBase(size,message))

Function ShiftRight(unsigned integer message, unsigned integer numberOfPositions)
    Return Floor(message / (size to the numberOfPositions power))

Function ShiftLeft(unsigned integer message, unsigned integer numberOfPositions)
    Return message * (size to the numberOfPositions power)

Function Decode(unsigned integer message)
    Let var buffer be a string buffer

    // Runs a number of times equal to LeftMostPosition(message) + 1
    Count position from LeftMostPosition(message) down through 0
        // Get the symbol from the left side of the message
        Let var index = ShiftRight(message, position)
        // Add the decoded character
        To buffer, add characters[index]
        // And then remove it from the incoming message
        Let message = message - ShiftLeft(index, position)

    Return contents of buffer

A more pseudocodey version.

// Maps some characters such that
//  0   ->'0'
//  9   ->'9'
//  10  ->'a'
//  35  ->'z'
//  36  ->'A'
//  61  ->'Z'
Let constant characters = List ('0'..'9', 'a'..'z', 'A'..'Z')
Let constant size = length of characters

Function LogBase(number base, number x)
    Return LogBase10(x) / LogBase10(base)

Function LeftMostPosition(unsigned integer message)
    Return Floor(LogBase(size,message))

Function ShiftRight(unsigned integer message, unsigned integer numberOfPositions)
    Return Floor(message / (size to the numberOfPositions power))

Function ShiftLeft(unsigned integer message, unsigned integer numberOfPositions)
    Return message * (size to the numberOfPositions power)

Function Decode(unsigned integer message)
    Let var buffer be a string buffer

    // Runs a number of times equal to LeftMostPosition(message) + 1
    Count position from LeftMostPosition(message) down through 0
        // Get the symbol from the left side of the message
        Let var index = ShiftRight(message, position)
        // Add the decoded character
        To buffer, add characters[index]
        // And then remove it from the incoming message
        Let message = message - ShiftLeft(index, position)

    Return contents of buffer
沉鱼一梦 2024-10-19 08:57:55

希望这有帮助。

// Define a set of allowable characters
private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Function declaration
private static function _convertBase($num)
{
    $base = strlen(self::$_characters); // Count the number of characters available
    $string = '';                       // Initialize an empty string

    // Start the iterator off as (num / character count). Continue until it is zero.
    for ($t = floor(log10($num) / log10($base)); $t >= 0; $t--) {
        $a = floor($num / pow($base, $t));              // Find the numeric (0-$base) position of the corresponding character.
        $string .= substr(self::$_characters, $a, 1);   // Pull that character out and add it to the return string
        $num = $num - ($a * pow($base, $t));            // Subtract it from $num
    }

    return $string;                    // Return the encoded string 
}

Hope this helps.

// Define a set of allowable characters
private static $_characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

// Function declaration
private static function _convertBase($num)
{
    $base = strlen(self::$_characters); // Count the number of characters available
    $string = '';                       // Initialize an empty string

    // Start the iterator off as (num / character count). Continue until it is zero.
    for ($t = floor(log10($num) / log10($base)); $t >= 0; $t--) {
        $a = floor($num / pow($base, $t));              // Find the numeric (0-$base) position of the corresponding character.
        $string .= substr(self::$_characters, $a, 1);   // Pull that character out and add it to the return string
        $num = $num - ($a * pow($base, $t));            // Subtract it from $num
    }

    return $string;                    // Return the encoded string 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文