1

I have a program that encodes integers into random looking, unique, maxiumum 5 character strings using a custom base array. I'd like to decode that string back to an integer. Not sure how to accomplish that.

Code:

function intToAlphaBaseN($n, $baseArray) {
 $l=count($baseArray);
 $s = '';
 for ($i = 1; $n >= 0 && $i < 6; $i++) {
  $s =  $baseArray[($n % pow($l, $i) / pow($l, $i - 1))].$s;
  $n -= pow($l, $i);
 }
 return $s;
}

$alpha=array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9');
$number = 1234;
echo("Number: " . $number . " Converted: " . intToAlphaBaseN($number, $alpha)); 

Output: Number: 1234 Converted: afu

Would like to be able to decode something like "afu".

Ted Scheckler
  • 1,389
  • 4
  • 16
  • 34
  • That's not a base encoding, nor how one operates. With how you 'encode' it, you won't be able to reverse it quite so easily. – Jon Jun 13 '14 at 19:24

3 Answers3

0

You may be reinventing the whell, and besides that, you should show us that you actually tried to implement it, before asking help (show some code).

I recommend reading the base_convert PHP documentation, and the first comments after the documentation. http://www.php.net/manual/en/function.base-convert.php

Otherwise here is an (untested) implementation of base decoding (after seeing Jon's comment, i am not so sure your function actually does base encoding):

function alphaBaseNToInt($alpha, $baseArray) {
    $result = 0;
    $l = count($baseArray);

    for ($i = strlen($alpha) - 1; $i >= 0; --$i)
        $result = $result * $l + array_search($alpha[$i], $baseArray);

    return $result;
}
Eloims
  • 5,106
  • 4
  • 25
  • 41
0

No, you can't decode string into number, because you don't know last value of $n in function.

For example, last array index is 0 ('a') and $i=strlen('afu');.

0=$n % pow($l, $i) / pow($l, $i - 1)

0=$n % 39304 / 1156

$n=[0,1155]+39304*c; where c=0,1,2,3... and $n is the last value of $n in cycle.

When you find last $n value, you can find full $number by summing pow($l, $i) in decrease cycle.

This full $n place between pow($l, 1) + pow($l, 2) and pow($l, 1) + pow($l, 2) + pow($l, 3).

CnapoB
  • 665
  • 1
  • 9
  • 16
0

try this

 function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}

function intToAlphaBaseN($n,$baseArray) {
    $l=count($baseArray);
    $s = '';
    for ($i = 1; bccomp($n , 0) > 0 ; $i = bcadd($i,1)) {
        $s =  $baseArray[bcdiv(bcmod($n , bcpow($l, $i)) , bcpow($l, bcsub($i , 1)))].$s;
        $n = bcsub($n,bcmul(bcdiv(bcmod($n , bcpow($l, $i)) , bcpow($l, bcsub($i , 1))),bcpow($l, bcsub($i , 1))));
        bcadd($i,1);
    }
    return $s;
}

function alphaBaseNToInt($alpha, $baseArray) {
    $result = 0;
    $l = count($baseArray);
    $alpha = str_split_unicode($alpha,1);
    $alpha = array_reverse($alpha);
    for ($i = count($alpha) - 1; $i >= 0; $i--) {
        $result = bcadd($result, bcmul(array_search($alpha[$i],$baseArray) , bcpow($l,$i)));
    }

    return $result;
}

$alpha=array(   '0','1','2','3','4','5','6','7','8','9',
                'A','B','C','Ç','D','E','F','G','Ğ','H','I','İ','J','K','L','M','N','O','Ö','P','Q','R','S','Ş','T','U','Ü','V','W','X','Y','Z'); 


 $listmet = intToAlphaBaseN($string,$alpha);


 var_dump($listmet);

  $listmet = alphaBaseNToInt($listmet,$alpha);
 var_dump($listmet);

i used bcmath library. because i am working very very big numbers. and used str_split_unicode for special turkish chars.

MC_delta_T
  • 596
  • 1
  • 9
  • 22