Search code examples
phpencodingencodebase-conversion

converting from base 10 to base 31 (working only with select characters)


I'd like to convert base 10 numbers to base 31

I would like to use only these characters: 23456789abcdefghjkmnpqrstuvwxyz

As you can see, 5 characters are excluded (i don't need these): 1 0 o l i

The function I have now is below but of course it doesn't work. When 2 is input it outputs 4. The output for tenTo31(2) should be 2

function tenTo31($num)
{
    $out   = "";
    $alpha = "23456789abcdefghjkmnpqrstuvwxyz";

    while($num > 30)
    {
        $r = $num % 31;
        $num = floor($num / 31) - 1;
        $out = $alpha[$r] . $out;
    }

    return $alpha[$num] . $out;
}

Any ideas on how to make this work?


Solution

  • This is a blind guess at what you want:

    $alpha = "yz23456789abcdefghjkmnpqrstuvwx";