| 1 |
<?php |
| 2 |
/** |
| 3 |
* This program is free software; you can redistribute it and/or modify |
| 4 |
* it under the terms of the GNU General Public License as published by |
| 5 |
* the Free Software Foundation; either version 2 of the License, or |
| 6 |
* (at your option) any later version. |
| 7 |
* |
| 8 |
* This program is distributed in the hope that it will be useful, |
| 9 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
* GNU General Public License for more details. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License along |
| 14 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 15 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 |
* http://www.gnu.org/copyleft/gpl.html |
| 17 |
* |
| 18 |
* @file |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace Wikimedia; |
| 22 |
|
| 23 |
/** |
| 24 |
* Convert an arbitrarily-long string from one numeric base to |
| 25 |
* another, optionally zero-padding to a minimum column width. |
| 26 |
* |
| 27 |
* Supports base 2 through 36; digit values 10-36 are represented |
| 28 |
* as lowercase letters a-z. Input is case-insensitive. |
| 29 |
* |
| 30 |
* @param string $input Input number |
| 31 |
* @param int $sourceBase Base of the input number |
| 32 |
* @param int $destBase Desired base of the output |
| 33 |
* @param int $pad Minimum number of digits in the output (pad with zeroes) |
| 34 |
* @param bool $lowercase Whether to output in lowercase or uppercase |
| 35 |
* @param string $engine Either "gmp", "bcmath", "php" or "auto" (default). |
| 36 |
* In the case of "auto", the other engines ("gmp" and "bcmath") are used in |
| 37 |
* the listed order in terms of preference if that PHP extension is actually loaded. |
| 38 |
* @return string|bool The output number as a string, or false on error |
| 39 |
*/ |
| 40 |
function base_convert( $input, $sourceBase, $destBase, $pad = 1, |
| 41 |
$lowercase = true, $engine = 'auto' |
| 42 |
) { |
| 43 |
$input = (string)$input; |
| 44 |
if ( |
| 45 |
$sourceBase < 2 || |
| 46 |
$sourceBase > 36 || |
| 47 |
$destBase < 2 || |
| 48 |
$destBase > 36 || |
| 49 |
$sourceBase != (int)$sourceBase || |
| 50 |
$destBase != (int)$destBase || |
| 51 |
$pad != (int)$pad || |
| 52 |
!preg_match( |
| 53 |
"/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i", |
| 54 |
$input |
| 55 |
) |
| 56 |
) { |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
static $baseChars = [ |
| 61 |
10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f', |
| 62 |
16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l', |
| 63 |
22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r', |
| 64 |
28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x', |
| 65 |
34 => 'y', 35 => 'z', |
| 66 |
|
| 67 |
'0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, |
| 68 |
'6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11, |
| 69 |
'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, |
| 70 |
'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, |
| 71 |
'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29, |
| 72 |
'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35 |
| 73 |
]; |
| 74 |
|
| 75 |
if ( extension_loaded( 'gmp' ) && ( $engine == 'auto' || $engine == 'gmp' ) ) { |
| 76 |
$result = gmp_strval( gmp_init( $input, $sourceBase ), $destBase ); |
| 77 |
} elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) { |
| 78 |
$decimal = '0'; |
| 79 |
foreach ( str_split( strtolower( $input ) ) as $char ) { |
| 80 |
$decimal = bcmul( $decimal, (string)$sourceBase ); |
| 81 |
$decimal = bcadd( $decimal, $baseChars[$char] ); |
| 82 |
} |
| 83 |
|
| 84 |
for ( $result = ''; bccomp( $decimal, '0' ); $decimal = bcdiv( $decimal, (string)$destBase, 0 ) ) { |
| 85 |
// As of PHP 7.2, bcmod can return a floating point value if bcscale is nonzero |
| 86 |
$result .= $baseChars[(int)bcmod( $decimal, (string)$destBase )]; |
| 87 |
} |
| 88 |
|
| 89 |
$result = strrev( $result ); |
| 90 |
} else { |
| 91 |
$inDigits = []; |
| 92 |
foreach ( str_split( strtolower( $input ) ) as $char ) { |
| 93 |
$inDigits[] = $baseChars[$char]; |
| 94 |
} |
| 95 |
|
| 96 |
// Iterate over the input, modulo-ing out an output digit |
| 97 |
// at a time until input is gone. |
| 98 |
$result = ''; |
| 99 |
while ( $inDigits ) { |
| 100 |
$work = 0; |
| 101 |
$workDigits = []; |
| 102 |
|
| 103 |
// Long division... |
| 104 |
foreach ( $inDigits as $digit ) { |
| 105 |
$work *= $sourceBase; |
| 106 |
$work += $digit; |
| 107 |
|
| 108 |
if ( $workDigits || $work >= $destBase ) { |
| 109 |
$workDigits[] = (int)( $work / $destBase ); |
| 110 |
} |
| 111 |
$work %= $destBase; |
| 112 |
} |
| 113 |
|
| 114 |
// All that division leaves us with a remainder, |
| 115 |
// which is conveniently our next output digit. |
| 116 |
$result .= $baseChars[$work]; |
| 117 |
|
| 118 |
// And we continue! |
| 119 |
$inDigits = $workDigits; |
| 120 |
} |
| 121 |
|
| 122 |
$result = strrev( $result ); |
| 123 |
} |
| 124 |
|
| 125 |
if ( !$lowercase ) { |
| 126 |
$result = strtoupper( $result ); |
| 127 |
} |
| 128 |
|
| 129 |
return str_pad( $result, $pad, '0', STR_PAD_LEFT ); |
| 130 |
} |