/** * Converts a base-10 number to an arbitrary base (from 2 to 36) * * @param string|int $number The number to convert * @param int $toBase The base to convert $number to * @return string * @throws \InvalidArgumentException if $toBase is outside the range 2 to 36 */ public static function convertFromBase10($number, $toBase) { if ($toBase < 2 || $toBase > 36) { throw new \InvalidArgumentException("Invalid `to base' ({$toBase})"); } $bn = new self($number); $number = $bn->abs()->getValue(); $digits = '0123456789abcdefghijklmnopqrstuvwxyz'; $outNumber = ''; $returnDigitCount = 0; while (bcdiv($number, bcpow($toBase, (string) $returnDigitCount)) > $toBase - 1) { $returnDigitCount++; } for ($i = $returnDigitCount; $i >= 0; $i--) { $pow = bcpow($toBase, (string) $i); $c = bcdiv($number, $pow); $number = bcsub($number, bcmul($c, $pow)); $outNumber .= $digits[(int) $c]; } return $outNumber; }