0

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
7
  • 1
    Looks like it's just counting in base 36. The counting doesn't appear to start from zero, but as far as I can tell there's nothing obscure going on.
    – r3mainer
    May 21, 2018 at 19:42
  • Thanks for the quick reply and answer. How do i achieve the opposite of your example? number to Q code?
    – kabus
    May 21, 2018 at 20:29
  • strtoupper(base_convert($decimal_number - 166244, 10, 36))
    – r3mainer
    May 21, 2018 at 20:34
  • 1
    Since there are lower case and upper case letters it is probably using base 62 (26 + 26 + 10)
    – Henry
    May 22, 2018 at 6:16
  • @squeamishossifrage i have updated my question. Your solution works for the first part, but when i want to convert for example 1277450 i will get NTF3 as result.
    – kabus
    May 22, 2018 at 6:23

1 Answer 1

1

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;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.