我怎么能转换这个功能?

时间:2011-03-11 21:54:50

标签: php

function getVideoName($in) {
    $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    $base = strlen($index);

    // Digital number  <<--  alphabet letter code
    $in = strrev($in);
    $out = 0;
    $len = strlen($in) - 1;
    for ($t = 0; $t <= $len; $t++) {
        $bcpow = bcpow($base, $len - $t);
        $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
    }

    $out = sprintf('%F', $out);
    $out = substr($out, 0, strpos($out, '.'));

    return $out;

}

此函数返回转换后的值 我如何将值转换回输入数字?

1 个答案:

答案 0 :(得分:2)

你可能无法做到这一点。代码看起来只是一种方式。

for ($t = 0; $t <= $len; $t++) {
// bcpow raises the first argument to the power of the second argument,
// the first argument being the length of the string, the second being
// the length minus one, minus the current position being inspected.
// This can make a pretty large number depending on the length of the string.
    $bcpow = bcpow($base, $len - $t);
// Then that number is multiplied by the position of 
// the currently inspected character in the string,
// as if it was in the key string given earlier,
// then that number is added to the running total.
    $out = $out + strpos($index, substr($in, $t, 1)) * $bcpow;
}
// Then it's formatted as a floating point number
$out = sprintf('%F', $out);
// and then truncated at the decimal.
$out = substr($out, 0, strpos($out, '.'));

这实际上是一种方法,因为撤消它需要知道字符串的长度和字符在其中的位置,如果你知道,你有原始的字符串!


该函数也略有错误,getVideoName('a')返回0. getVideoName('aaaaaaaaaa')也是如此。 getVideoName('d')返回3,getVideoName('da')也是如此。

编号是可预测的,并遵循一种模式。它可能被称为deterministic,甚至。如果有来自不知道公式的局外人的足够输入,则可能重建或猜测输出......但这将是一个非常耗时且烦人的过程。