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

__construct(mixed $sourceBase, mixed $targetBase)

Creates a new instance of BaseConverter.

static string|false
baseConvert(string $number, mixed $fromBase, mixed $toBase, int $precision = -1)

Converts the provided number from base to another.

string|false
convert(string $number)

Converts the number provided as a string from source base to target base.

void
setPrecision(int $precision)

Sets the precision for inaccurate fraction conversions.

array
convertInteger(array $number)

Converts the integer part of a number.

array
convertFractions(array $number)

Converts the fractional part of a number.

Details

__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.

Parameters

mixed $sourceBase Number base used by the provided numbers
mixed $targetBase Number base used by the returned numbers

See also

NumberBase::__construct

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().

Parameters

string $number The number to convert
mixed $fromBase Number base used by the provided number
mixed $toBase Number base used by the returned number
int $precision Precision for inaccurate conversion

Return Value

string|false The converted number or false on error

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.

Parameters

string $number The number to convert

Return Value

string|false The converted number or false on error

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.

Parameters

int $precision Precision used for inaccurate conversions

Return Value

void

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.

Parameters

array $number Array of digits representing the integer part

Return Value

array Digits for the converted number

Exceptions

InvalidDigitException If the integer part contains invalid digits

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.

Parameters

array $number Array of digits representing the fractional part

Return Value

array Digits for the converted number

Exceptions

InvalidDigitException If the fractional part contain invalid digits