-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseConvert.php
126 lines (89 loc) · 3.33 KB
/
BaseConvert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
// 4,294,967,295
class BaseConvert
{
private $charset;
private $base;
public $errors;
public function __construct()
{
$this->charset = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$this->charset = '0123456789ABCDEGHJKLMNPQRTVWXYZ';
// I, O, U, S, F
$this->base = mb_strlen($this->charset);
}
public function setCharSet($charset)
{
$this->charset = $charset;
$this->base = mb_strlen($this->charset);
}
public function setBase($base)
{
$this->base = $base;
if($this->base > mb_strlen($this->charset)) {
die('Base exceeds charset.');
}
}
private function validateBase()
{
// Make sure that the base number is valid.
if ($this->base < 2 | $this->base > 36 | $this->base == 10) {
$this->errors[] = 'BASE must be in the range 2-9 or 11-36.';
return false;
}
return true;
}
public function encode($decimal)
{
if(!$this->validateBase()) {
return false;
}
// Checking that $decimal is a positive integer between 1 and 16 digits long.
if (!preg_match('(^[0-9]{1,16}$)', trim($decimal))) {
$this->errors[] = 'Decimal must be a positive integer.';
return false;
};
$string = '';
// Set up the list of possible output characters, then chop off any characters beyond the limit specified by $this->base.
$charset = substr($this->charset, 0, $this->base);
$decimal = trim((int)$decimal);
do {
$remainder = ($decimal % $this->base);
// Extract the character that corresponds to this remainder and add it to the front of the output string.
$char = substr($charset, $remainder, 1);
$string = "$char$string";
// Reduce the decimal value by the number we have just processed. This is done by subtracting $remainder then dividing by $this->base.
$decimal = ($decimal - $remainder) / $this->base;
} while ($decimal > 0);
return $string;
}
public function decode($string)
{
if(!$this->validateBase()) {
return false;
}
// Check string is not empty.
if(empty($string)) {
$this->errors[] = 'Input string is empty.';
return false;
}
$decimal = 0;
// Set up the list of possible output characters, then chop off any characters beyond the limit specified by $this->base.
$charset = substr($this->charset, 0, $this->base);
$string = trim($string);
do {
// Extract the first character from the input string, then remove it from the string.
$char = substr($string, 0, 1);
$string = substr($string, 1);
// Obtain the position of $char in $charset.
$pos = strpos($charset, $char);
if ($pos === false) {
$this->errors[] = "Illegal character ($char) in INPUT string";
return false;
}
// Increment the decimal value by the value of the character we have just extracted from the input string.
$decimal = ($decimal * $this->base) + $pos;
} while($string <> null);
return $decimal;
}
}