|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of ocubom/base-convert |
|
5
|
|
|
* |
|
6
|
|
|
* © Oscar Cubo Medina <https://ocubom.github.io> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Ocubom\Math; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Convert a number between arbitrary bases. |
|
16
|
|
|
* |
|
17
|
|
|
* Supports large arbitrary numbers by using a safe php implementation |
|
18
|
|
|
* |
|
19
|
|
|
* @see http://php.net/manual/en/function.base-convert.php |
|
20
|
|
|
* @see http://php.net/manual/en/function.base-convert.php#109660 |
|
21
|
|
|
* |
|
22
|
|
|
* @param int|string $number The number to convert |
|
23
|
|
|
* @param BaseInterface|int|string $source Original base for number |
|
24
|
|
|
* @param BaseInterface|int|string $target Desired base for number |
|
25
|
|
|
*/ |
|
26
|
|
|
function base_convert($number, $source, $target): string |
|
27
|
|
|
{ |
|
28
|
6593 |
|
return Base::convert($number, $source, $target); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Convert a number to crockford base32 encoding. |
|
33
|
|
|
* |
|
34
|
|
|
* @param int|string $number The number to convert |
|
35
|
|
|
* @param BaseInterface|int|string $base The base of $number |
|
36
|
|
|
* @param bool $checksum Include a checksum |
|
37
|
|
|
*/ |
|
38
|
|
|
function crockford_encode($number, $base, bool $checksum = false): string |
|
39
|
|
|
{ |
|
40
|
16 |
|
return Crockford::encode($number, $base, $checksum); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Convert a number from crockford base32 encoding. |
|
45
|
|
|
* |
|
46
|
|
|
* @param int|string $number The crockford number to convert |
|
47
|
|
|
* @param BaseInterface|int|string $base The base to convert $number |
|
48
|
|
|
* @param bool $checksum Includes $number a checksum? |
|
49
|
|
|
*/ |
|
50
|
|
|
function crockford_decode($number, $base, bool $checksum = false): string |
|
51
|
|
|
{ |
|
52
|
51 |
|
return Crockford::decode($number, $base, $checksum); |
|
53
|
|
|
} |
|
54
|
|
|
|