BaseConverter
class BaseConverter implements Converter (View source)
Arbitrary precision number base converter.
BaseConverter provides convenience to number conversion by providing a method that accepts numbers as strings in addition to selecting the appropriate number conversion strategy based on the provided number bases (which may also be provided as constructor arguments for NumberBase instead of instances of the said class).
BaseConverter can also be used as a simple replacement for PHP's built in
base_convert()
via the provided static method that accepts arguments
similar to the built in function in addition to providing the extra features
of this library (such as arbitrary precision conversion and support for
fractions).
Methods
Creates a new instance of BaseConverter.
Converts the provided number from base to another.
Converts the number provided as a string from source base to target base.
Sets the precision for inaccurate fraction conversions.
Converts the integer part of a number.
Converts the fractional part of a number.
Details
at line 52
__construct(mixed $sourceBase, mixed $targetBase)
Creates a new instance of BaseConverter.
The source and target number bases can be provided either as an instance of the NumberBase class or as constructor arguments that are provided to the NumberBase class.
The constructor will select the most optimal conversion strategy based on the provided number bases.
at line 83
static string|false
baseConvert(string $number, mixed $fromBase, mixed $toBase, int $precision = -1)
Converts the provided number from base to another.
This method provides a convenient replacement to PHP's built in
base_convert()
. The number bases are simply passed along to the
constructor, which means they can be instances of NumberBase class or
constructor parameters for that class.
Note that due to the way the constructor parameters for NumberBase work,
this method can be used exactly the same way as base_convert()
.
at line 109
string|false
convert(string $number)
Converts the number provided as a string from source base to target base.
This method provides convenient conversions by accepting the number as a string. The number may optionally be preceded by a plus or minus sign which is prepended to the result as well. The number may also have a period, which separates the integer part and the fractional part.
Due to the special meaning of +
, -
and .
, it is not recommended to
use this method to convert numbers when using number bases that have a
meaning for these characters (such as base64).
If the number contains invalid characters, the method will return false instead.
at line 150
void
setPrecision(int $precision)
Sets the precision for inaccurate fraction conversions.
The fractions cannot always be accurately converted from base to another, since they may represent a fraction that cannot be represented in another number base. The precision value is used to determine the number of digits in the fractional part, if the number cannot be accurately converted or if it is not feasible to determine that.
If the precision is positive, it defines the maximum number of digits in the fractional part. If the value is 0, the converted number will have at least as many digits in the fractional part as it takes to represent the number in the same accuracy as the original number. A negative number will simply increase the number of digits.
Note that the fractional part may have fewer digits than what is required by the precision if it can be accurately represented using fewer digits.
at line 155
array
convertInteger(array $number)
Converts the integer part of a number.
The integer part should be provided as an array of digits with least significant digit first. Any invalid digit in the array will cause an exception to be thrown. The return value will be a similar array of digits, except converted to the target number base.
at line 160
array
convertFractions(array $number)
Converts the fractional part of a number.
The fractional part should be provided as an array of digits with least significant digit first. Any invalid digit in the array will cause an exception to be thrown. The return value will be a similar array of digits, except converted to the target number base.