HH\Lib\Math\to_base

Meta Engineer?

This is available as Math\to_base in the www repository.

Converts the given non-negative number into the given base, using letters a-z for digits when $to_base > 10

namespace HH\Lib\Math;

function to_base(
  int $number,
  int $to_base,
): string;

To base convert a string to an int, see Math\from_base().

Parameters

  • int $number
  • int $to_base

Returns

  • string

Examples

$in = 30;

$base_1 = 10;
$out_1 = Math\to_base($in, $base_1);
echo "$in to base $base_1 is $out_1 \n";

$base_2 = 16;
$out_2 = Math\to_base($in, $base_2);
echo "$in to base $base_2 is $out_2 \n";

$base_3 = 2;
$out_3 = Math\to_base($in, $base_3);
echo "$in to base $base_3 is $out_3 \n";