Search code examples
algorithmencodingdecoding

algorithm with Q codes? What algorithm is used here?


Does anyone know what kind of algorithm is used to generate the Q codes?

200000 : Q1O
200001 : Q1P
200002 : Q1Q
200003 : Q1R
200004 : Q1S
200005 : Q1T
200006 : Q1U
200007 : Q1V
200008 : Q1W
200009 : Q1X
200010 : Q1Y
200011 : Q1Z
200012 : Q20
200013 : Q21
200014 : Q22
200015 : Q23

Is this base convert? I have added some extra lines to for example

1127745 : 4Jnr
1277450 : 5mk2
1277451 : 5mk3
1277452 : 5mk4
1277453 : 5mk5
1277454 : 5mk6
1277455 : 5mk7
1277456 : 5mk8
1277457 : 5mk9

Solution

  • The solution is base62 encoding

     <?php
    
    class Base62 {
        static $base = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        static $base_size = 62;
        static $base_flip = array();
    
        static function get_base_flip() {
            if(self::$base_flip == array())
                self::$base_flip = array_flip(str_split(Base62::$base));
            return self::$base_flip;
        }
    
        static function encode($val) {
            $str = '';
            while($val>0) {
                $mod = $val % self::$base_size;
                $str = self::$base[$mod].$str;  
                $val = ($val-$mod)/self::$base_size;
            }
            return $str;
        }
    
        static function decode($str) {
            $val = 0;
            $base_arr = self::get_base_flip();
            $str_arr = array_reverse(str_split($str)); 
            foreach($str_arr as $key=>$value){ 
                $val += $base_arr[$value]*(pow(self::$base_size,$key));
            }   
            return $val;
        }
    
    }