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
kabus
  • 899
  • 1
  • 11
  • 20
  • 1
    Looks like it's just [counting in base 36](https://ideone.com/NrA0mq). 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 '18 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 '18 at 20:29
  • `strtoupper(base_convert($decimal_number - 166244, 10, 36))` – r3mainer May 21 '18 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 '18 at 06: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 '18 at 06:23
  • @Henry i tried https://base62.io/ to regenerate for example 1277450 and the result is 11Q8lXgthA. Can you provide an example? – kabus May 22 '18 at 06:39
  • @kabus Henry is correct, it's base62. The tool at base62.io treats the input as a string of bytes, so it won't encode numbers properly. In any case, the result you got is obviously wrong — a 10-digit number in base 62 must be at least 62^9, which is far greater than 1277450. – r3mainer May 22 '18 at 08:01

1 Answers1

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

}
kabus
  • 899
  • 1
  • 11
  • 20