format
[PHP-P10.git] / Uplink / Numerics.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  Philipp Kreil (pk910)                       *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      *
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  *
20  *  Uplink/Numerics.class.php
21  *
22  * P10 numeric functions
23  *
24  */
25
26 class Numerics {
27         private static $base64chars = array(
28           'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
29           'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
30           'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
31           'w','x','y','z','0','1','2','3','4','5','6','7','8','9','[',']'
32           );
33           private static $base64charsLength = 64;
34
35           public static function intToNum($int, $length) {
36                 $numeric = "";
37                 for($pos = $length-1; $pos >= 0; $pos--) {
38                         //current position represents floor($int / ($base64charsLength ^ $pos))
39                         $base = 1;
40                         for($i = 0; $i < $pos; $i++) {
41                                 $base = $base * self::$base64charsLength;
42                         }
43                         $posValue = floor($int / $base);
44                         $int -= $posValue * $base;
45                         //get the char representing $posValue
46                         $posChar = self::$base64chars[$posValue];
47                         $numeric .= $posChar;
48                 }
49
50                 return $numeric;
51           }
52
53           public static function numToInt($numeric) {
54                 $base = 1;
55                 $int = 0;
56                 for($pos = strlen($numeric)-1; $pos >= 0; $pos--) {
57                         $posValue = array_search($numeric[$pos], self::$base64chars);
58                         $int = ($posValue * $base);
59                         $base = $base * self::$base64charsLength;
60                 }
61                 return $int;
62           }
63
64 }
65
66 ?>