1
|
|
|
<?php |
2
|
|
|
namespace Fwolf\Util\BaseConverter; |
3
|
|
|
|
4
|
|
|
use Fwolf\Base\Singleton\SingleInstanceTrait; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Notice: BC Math or GMP extension maybe not loaded, so test cannot cover all. |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright 2006-2016 Fwolf |
10
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
11
|
|
|
*/ |
12
|
|
|
class BaseConverter implements BaseConverterInterface |
13
|
|
|
{ |
14
|
|
|
use SingleInstanceTrait; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Number to char |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $map = [ |
23
|
|
|
'0', |
24
|
|
|
'1', |
25
|
|
|
'2', |
26
|
|
|
'3', |
27
|
|
|
'4', |
28
|
|
|
'5', |
29
|
|
|
'6', |
30
|
|
|
'7', |
31
|
|
|
'8', |
32
|
|
|
'9', |
33
|
|
|
'a', |
34
|
|
|
'b', |
35
|
|
|
'c', |
36
|
|
|
'd', |
37
|
|
|
'e', |
38
|
|
|
'f', |
39
|
|
|
'g', |
40
|
|
|
'h', |
41
|
|
|
'i', |
42
|
|
|
'j', |
43
|
|
|
'k', |
44
|
|
|
'l', |
45
|
|
|
'm', |
46
|
|
|
'n', |
47
|
|
|
'o', |
48
|
|
|
'p', |
49
|
|
|
'q', |
50
|
|
|
'r', |
51
|
|
|
's', |
52
|
|
|
't', |
53
|
|
|
'u', |
54
|
|
|
'v', |
55
|
|
|
'w', |
56
|
|
|
'x', |
57
|
|
|
'y', |
58
|
|
|
'z', |
59
|
|
|
'A', |
60
|
|
|
'B', |
61
|
|
|
'C', |
62
|
|
|
'D', |
63
|
|
|
'E', |
64
|
|
|
'F', |
65
|
|
|
'G', |
66
|
|
|
'H', |
67
|
|
|
'I', |
68
|
|
|
'J', |
69
|
|
|
'K', |
70
|
|
|
'L', |
71
|
|
|
'M', |
72
|
|
|
'N', |
73
|
|
|
'O', |
74
|
|
|
'P', |
75
|
|
|
'Q', |
76
|
|
|
'R', |
77
|
|
|
'S', |
78
|
|
|
'T', |
79
|
|
|
'U', |
80
|
|
|
'V', |
81
|
|
|
'W', |
82
|
|
|
'X', |
83
|
|
|
'Y', |
84
|
|
|
'Z', |
85
|
|
|
]; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Char to number |
89
|
|
|
* |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected $reverseMap = [ |
93
|
|
|
'0' => 0, |
94
|
|
|
'1' => 1, |
95
|
|
|
'2' => 2, |
96
|
|
|
'3' => 3, |
97
|
|
|
'4' => 4, |
98
|
|
|
'5' => 5, |
99
|
|
|
'6' => 6, |
100
|
|
|
'7' => 7, |
101
|
|
|
'8' => 8, |
102
|
|
|
'9' => 9, |
103
|
|
|
'a' => 10, |
104
|
|
|
'b' => 11, |
105
|
|
|
'c' => 12, |
106
|
|
|
'd' => 13, |
107
|
|
|
'e' => 14, |
108
|
|
|
'f' => 15, |
109
|
|
|
'g' => 16, |
110
|
|
|
'h' => 17, |
111
|
|
|
'i' => 18, |
112
|
|
|
'j' => 19, |
113
|
|
|
'k' => 20, |
114
|
|
|
'l' => 21, |
115
|
|
|
'm' => 22, |
116
|
|
|
'n' => 23, |
117
|
|
|
'o' => 24, |
118
|
|
|
'p' => 25, |
119
|
|
|
'q' => 26, |
120
|
|
|
'r' => 27, |
121
|
|
|
's' => 28, |
122
|
|
|
't' => 29, |
123
|
|
|
'u' => 30, |
124
|
|
|
'v' => 31, |
125
|
|
|
'w' => 32, |
126
|
|
|
'x' => 33, |
127
|
|
|
'y' => 34, |
128
|
|
|
'z' => 35, |
129
|
|
|
'A' => 36, |
130
|
|
|
'B' => 37, |
131
|
|
|
'C' => 38, |
132
|
|
|
'D' => 39, |
133
|
|
|
'E' => 40, |
134
|
|
|
'F' => 41, |
135
|
|
|
'G' => 42, |
136
|
|
|
'H' => 43, |
137
|
|
|
'I' => 44, |
138
|
|
|
'J' => 45, |
139
|
|
|
'K' => 46, |
140
|
|
|
'L' => 47, |
141
|
|
|
'M' => 48, |
142
|
|
|
'N' => 49, |
143
|
|
|
'O' => 50, |
144
|
|
|
'P' => 51, |
145
|
|
|
'Q' => 52, |
146
|
|
|
'R' => 53, |
147
|
|
|
'S' => 54, |
148
|
|
|
'T' => 55, |
149
|
|
|
'U' => 56, |
150
|
|
|
'V' => 57, |
151
|
|
|
'W' => 58, |
152
|
|
|
'X' => 59, |
153
|
|
|
'Y' => 60, |
154
|
|
|
'Z' => 61, |
155
|
|
|
]; |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Number equal or larger than 100000000000000(1.0E+14) will represent by |
159
|
|
|
* Scientific Notation(科学记数法), cause base_convert() loose precision. |
160
|
|
|
* So for larger number, use BC Math or GMP. |
161
|
|
|
* |
162
|
|
|
* This array is used to check number string length per base, if string is |
163
|
|
|
* longer than the array value, it should not use build-in base_convert(). |
164
|
|
|
* |
165
|
|
|
* @link https://gist.github.com/fwolf/7250392 |
166
|
|
|
*/ |
167
|
|
|
protected $safeLengthMap = [ |
168
|
|
|
2 => 46, // 2^46 = 70368744177664 |
|
|
|
|
169
|
|
|
3 => 29, // 3^29 = 68630377364883 |
|
|
|
|
170
|
|
|
4 => 23, // 4^23 = 70368744177664 |
|
|
|
|
171
|
|
|
5 => 20, // 5^20 = 95367431640625 |
|
|
|
|
172
|
|
|
6 => 17, // 6^17 = 16926659444736 |
|
|
|
|
173
|
|
|
7 => 16, // 7^16 = 33232930569601 |
|
|
|
|
174
|
|
|
8 => 15, // 8^15 = 35184372088832 |
|
|
|
|
175
|
|
|
9 => 14, // 9^14 = 22876792454961 |
|
|
|
|
176
|
|
|
10 => 13, // 10^13 = 10000000000000 |
|
|
|
|
177
|
|
|
11 => 13, // 11^13 = 34522712143931 |
|
|
|
|
178
|
|
|
12 => 12, // 12^12 = 8916100448256 |
|
|
|
|
179
|
|
|
13 => 12, // 13^12 = 23298085122481 |
|
|
|
|
180
|
|
|
14 => 12, // 14^12 = 56693912375296 |
|
|
|
|
181
|
|
|
15 => 11, // 15^11 = 8649755859375 |
|
|
|
|
182
|
|
|
16 => 11, // 16^11 = 17592186044416 |
|
|
|
|
183
|
|
|
17 => 11, // 17^11 = 34271896307633, |
|
|
|
|
184
|
|
|
18 => 11, // 18^11 = 64268410079232, |
|
|
|
|
185
|
|
|
19 => 11, // 19^10 = 6131066257801, |
|
|
|
|
186
|
|
|
20 => 10, // 20^10 = 10240000000000, |
|
|
|
|
187
|
|
|
21 => 10, // 21^10 = 16679880978201, |
|
|
|
|
188
|
|
|
22 => 10, // 22^10 = 26559922791424, |
|
|
|
|
189
|
|
|
23 => 10, // 23^10 = 41426511213649, |
|
|
|
|
190
|
|
|
24 => 10, // 24^10 = 63403380965376, |
|
|
|
|
191
|
|
|
25 => 10, // 25^10 = 95367431640625, |
|
|
|
|
192
|
|
|
26 => 9, // 26^9 = 5429503678976, |
|
|
|
|
193
|
|
|
27 => 9, // 27^9 = 7625597484987, |
|
|
|
|
194
|
|
|
28 => 9, // 28^9 = 10578455953408, |
|
|
|
|
195
|
|
|
29 => 9, // 29^9 = 14507145975869, |
|
|
|
|
196
|
|
|
30 => 9, // 30^9 = 19683000000000, |
|
|
|
|
197
|
|
|
31 => 9, // 31^9 = 26439622160671, |
|
|
|
|
198
|
|
|
32 => 9, // 32^9 = 35184372088832, |
|
|
|
|
199
|
|
|
33 => 9, // 33^9 = 46411484401953, |
|
|
|
|
200
|
|
|
34 => 9, // 34^9 = 60716992766464, |
|
|
|
|
201
|
|
|
35 => 9, // 35^9 = 78815638671875, |
|
|
|
|
202
|
|
|
36 => 8, // 36^8 = 2821109907456, |
|
|
|
|
203
|
|
|
37 => 8, // 37^8 = 3512479453921, |
|
|
|
|
204
|
|
|
38 => 8, // 38^8 = 4347792138496, |
|
|
|
|
205
|
|
|
39 => 8, // 39^8 = 5352009260481, |
|
|
|
|
206
|
|
|
40 => 8, // 40^8 = 6553600000000, |
|
|
|
|
207
|
|
|
41 => 8, // 41^8 = 7984925229121, |
|
|
|
|
208
|
|
|
42 => 8, // 42^8 = 9682651996416, |
|
|
|
|
209
|
|
|
43 => 8, // 43^8 = 11688200277601, |
|
|
|
|
210
|
|
|
44 => 8, // 44^8 = 14048223625216, |
|
|
|
|
211
|
|
|
45 => 8, // 45^8 = 16815125390625, |
|
|
|
|
212
|
|
|
46 => 8, // 46^8 = 20047612231936, |
|
|
|
|
213
|
|
|
47 => 8, // 47^8 = 23811286661761, |
|
|
|
|
214
|
|
|
48 => 8, // 48^8 = 28179280429056, |
|
|
|
|
215
|
|
|
49 => 8, // 49^8 = 33232930569601, |
|
|
|
|
216
|
|
|
50 => 8, // 50^8 = 39062500000000, |
|
|
|
|
217
|
|
|
51 => 8, // 51^8 = 45767944570401, |
|
|
|
|
218
|
|
|
52 => 8, // 52^8 = 53459728531456, |
|
|
|
|
219
|
|
|
53 => 8, // 53^8 = 62259690411361, |
|
|
|
|
220
|
|
|
54 => 8, // 54^8 = 72301961339136, |
|
|
|
|
221
|
|
|
55 => 8, // 55^8 = 83733937890625, |
|
|
|
|
222
|
|
|
56 => 8, // 56^8 = 96717311574016, |
|
|
|
|
223
|
|
|
57 => 7, // 57^7 = 1954897493193, |
|
|
|
|
224
|
|
|
58 => 7, // 58^7 = 2207984167552, |
|
|
|
|
225
|
|
|
59 => 7, // 59^7 = 2488651484819, |
|
|
|
|
226
|
|
|
60 => 7, // 60^7 = 2799360000000, |
|
|
|
|
227
|
|
|
61 => 7, // 61^7 = 3142742836021, |
|
|
|
|
228
|
|
|
62 => 7, // 62^7 = 3521614606208, |
|
|
|
|
229
|
|
|
]; |
230
|
|
|
|
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* {@inheritdoc} |
234
|
|
|
* |
235
|
|
|
* @throws \InvalidArgumentException |
236
|
|
|
* @throws \Exception |
237
|
|
|
*/ |
238
|
|
|
public function convert($number, $fromBase, $toBase) |
239
|
|
|
{ |
240
|
|
|
$min = min($fromBase, $toBase); |
241
|
|
|
$max = max($fromBase, $toBase); |
242
|
|
|
if (2 > $min || 62 < $max) { |
243
|
|
|
throw new \InvalidArgumentException('Base must between 2 and 62.'); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
|
247
|
|
|
$number = trim((string)$number); |
248
|
|
|
if (empty($number)) { |
249
|
|
|
return '0'; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
// Simple convert use build-in base_convert() |
254
|
|
|
if (36 >= $fromBase && 36 >= $toBase && |
255
|
|
|
strlen($number) <= $this->safeLengthMap[$fromBase] |
256
|
|
|
) { |
257
|
|
|
return strtolower(base_convert($number, $fromBase, $toBase)); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
$number = ltrim($number, '0'); // GMP treat leading 0 different. |
262
|
|
|
|
263
|
|
|
// Convert using BC Math or GMP, preferred GMP which is faster |
264
|
|
|
// In PHP 5.3.2, gmp base was extended to 2~62 |
265
|
|
|
if (extension_loaded('gmp')) { |
266
|
|
|
return $this->convertWithGmp($number, $fromBase, $toBase); |
267
|
|
|
|
268
|
|
|
} elseif (extension_loaded('bcmath')) { |
269
|
|
|
return $this->convertWithBcmath($number, $fromBase, $toBase); |
270
|
|
|
|
271
|
|
|
} else { |
272
|
|
|
throw new \Exception( |
273
|
|
|
'Number too large and BC Math or GMP not loaded.' |
274
|
|
|
); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Convert number string base using BC Math |
281
|
|
|
* |
282
|
|
|
* @param string $number |
283
|
|
|
* @param int $fromBase |
284
|
|
|
* @param int $toBase |
285
|
|
|
* @return string |
286
|
|
|
*/ |
287
|
|
|
protected function convertWithBcmath($number, $fromBase, $toBase) |
288
|
|
|
{ |
289
|
|
|
if (10 == $fromBase) { |
290
|
|
|
$base10 = $number; |
291
|
|
|
} else { |
292
|
|
|
$base10 = 0; |
293
|
|
|
for ($i = 0, $j = strlen($number); $i < $j; $i++) { |
294
|
|
|
$num = $this->reverseMap[$number{$i}]; |
295
|
|
|
$base10 = bcadd($num, bcmul($base10, $fromBase)); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
if (10 == $toBase) { |
300
|
|
|
return $base10; |
301
|
|
|
} else { |
302
|
|
|
$baseN = ''; |
303
|
|
|
while (0 < bccomp($base10, '0', 0)) { |
304
|
|
|
$remainder = intval(bcmod($base10, $toBase)); |
305
|
|
|
$baseN = $this->map[$remainder] . $baseN; |
306
|
|
|
$base10 = bcdiv($base10, $toBase, 0); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return $baseN; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Convert number string base using GMP gmp_strval() |
316
|
|
|
* |
317
|
|
|
* @param string $number |
318
|
|
|
* @param int $fromBase |
319
|
|
|
* @param int $toBase |
320
|
|
|
* @return string |
321
|
|
|
*/ |
322
|
|
|
protected function convertWithGmp($number, $fromBase, $toBase) |
323
|
|
|
{ |
324
|
|
|
// GMP use 0-9a-z for base 11~36, and 0-9A-Za-z for base 37~62, so we |
325
|
|
|
// need swap upper and lower case. |
326
|
|
|
// @link http://stackoverflow.com/questions/2259666 |
327
|
|
|
if (36 < $fromBase) { |
328
|
|
|
$number = strtolower($number) ^ strtoupper($number) ^ $number; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$number = gmp_strval(gmp_init($number, $fromBase), $toBase); |
332
|
|
|
|
333
|
|
|
if (36 < $toBase) { |
334
|
|
|
$number = strtolower($number) ^ strtoupper($number) ^ $number; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
return $number; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.