12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- declare(strict_types=1);
- const ErrInBase = "input base must be >= 2";
- const ErrOutBase = "output base must be >= 2";
- const ErrDigits = "all digits must satisfy 0 <= d < input base";
- function rebase(int $fromBase, array $digits, int $toBase): array {
- if ($fromBase < 2) {
- throw new \InvalidArgumentException(ErrInBase);
- }
- if ($toBase < 2) {
- throw new \InvalidArgumentException(ErrOutBase);
- }
- if ($fromBase === $toBase) {
- return $digits;
- }
- $rev = array_reverse($digits);
- $value = 0;
- $pow = 1;
- foreach ($rev as $digit) {
- if ($digit < 0 || $digit >= $fromBase) {
- throw new \InvalidArgumentException(ErrDigits);
- }
- $value += $digit * $pow;
- $pow *= $fromBase;
- }
- $reversed = [];
- $current = $value;
- while (TRUE) {
- $reversed[] = $current % $toBase;
- $current = (int) ($current / $toBase);
- if ($current === 0) {
- break;
- }
- }
- $out = array_reverse($reversed);
- return $out;
- }
|