Download the PHP package riimu/kit-baseconversion without Composer
On this page you can find all versions of the php package riimu/kit-baseconversion. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package kit-baseconversion
Arbitrary Precision Base Converter
BaseConversion is a PHP library for converting number bases, similar to PHP's
built in function base_convert()
. However, unlike the built in function, this
library is not limited by 32 bit integers and is capable of converting numbers
of arbitrary precision. This library also supports conversion of fractions and
allows more customization in terms of number bases.
In order to optimize the conversion of large numbers, this library also employs two different conversion strategies. In some cases, it's possible to convert numbers simply by replacing the digits with digits from the other base (e.g. when converting from base 2 to base 16). This is considerably faster than the other strategy, which simply calculates the new number using arbitrary precision integer arithmetic.
The API documentation, which can be generated using Apigen, can be read online at: http://kit.riimu.net/api/baseconversion/
Requirements
- The minimum supported PHP version is 5.6
- The library depends on the following PHP Extensions
gmp
(only required IDN support)
Installation
Installation with Composer
The easiest way to install this library is to use Composer to handle your dependencies. In order to install this library via Composer, simply follow these two steps:
-
Acquire the
composer.phar
by running the Composer Command-line installation in your project root. - Once you have run the installation script, you should have the
composer.phar
file in you project root and you can run the following command:
After installing this library via Composer, you can load the library by
including the vendor/autoload.php
file that was generated by Composer during
the installation.
Adding the library as a dependency
If you are already familiar with how to use Composer, you may alternatively add
the library as a dependency by adding the following composer.json
file to your
project and running the composer install
command:
Manual installation
If you do not wish to use Composer to load the library, you may also download
the library manually by downloading the latest release
and extracting the src
folder to your project. You may then include the
provided src/autoload.php
file to load the library classes.
Usage
The most convenient way to use this library is via the baseConvert()
static
method provided by the BaseConverter
class. In most cases, it works the same
way as base_convert()
does. For example:
The method accepts negative numbers and fractions in the same way. An optional fourth parameter can be used to define the precision for the conversion. For example:
The static method is simply a convenient wrapper for creating an instance of
BaseConvert
and calling the setPrecision()
and convert()
methods. If you
need to convert multiple numbers, it's more efficient to call the object in a
non static manner. For example:
If the provided number contains invalid digits that are not part of the defined number base, the method will return false instead.
Converting Fractions
While this library does support conversion of fractions, it's important to understand that fractions cannot always be converted accurately from number base to another the same way that integers can be converted. This is result of the fact that not all fractions can be represented in another number base.
For example, let's say we have the number 0.1 in base 3. This equals the same as 1/3 in base 10. However, if you were to represent 1/3 as a decimal number, you would get an infinitely repeating '0.3333...'. For example:
Due to this behavior, it is possible to set the precision used for inaccurate fraction conversions. As can be seen in the previous example, the precision value defines the maximum number of digits in the resulting number. The result may have less digits, however, if the number can be accurately converted using a small number of digits. The precision may also be completely ignored, if the converter knows, that it can accurately convert the fractions.
The precision value also has an alternative definition. If the precision is 0 or a negative number, then the maximum number of digits in the resulting number is based on the precision of the original number. If the precision is 0, the resulting number will have as many digits as it takes to represent the number in the same precision as the original number. A negative number will simply increase the number of digits in addition to that. For example:
In the previous example, the original number is 0.A7
in the base 16. A base 16
number with two digits in the fractional part can represent a number up to
accuracy of 1/(16 * 16) == 1/256
. To represent the the fractional part in the
same accuracy in base 10, we need at least 3 digits, because two digit can only
represent numbers up to accuracy of 1/100
.
The default precision value used by the library is -1
. It is also important
to note that the last digit is not rounded (due to the fact that it would
cause inconsistent results in some cases).
Case Sensitivity
In order to make user interaction with the library more convenient, the library
treats all numbers in a case insensitive manner, unless the number base
prohibits that. For example, the base 16 can be treated in a case insensitive
manner, because it only defines the value for the digits 0-9A-F
. However,
base 62 cannot be treated in a case insensitive manner, because letters like
A
and a
have a different value.
The returned numbers will always respect the character case defined by the number base. For example:
Customizing Number Bases
One of the features of this library is that allows much better customization of
number bases than base_convert()
. In most cases, you will probably define the
number base using a simple integer such as 10
or 16
. However, there is no
limit to the size of that integer. For example:
For large number bases, however, the digits are simply represented by a string
that consists of #
and the value for the digit. Whenever the number base is
defined using an integer, the digits follow the following rules:
- Bases equal or smaller than 62 use digits from the string
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
- A base 64 number uses digits from the base64 standard, i.e.
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
- Other bases equal or smaller than 256 use bytes as digits with byte value indicating the digit value.
- Large bases use strings for digits that consist of
#
and the value for the digit (the length of the string depends on the greatest digit value).
In addition to defining the number base using an integer, it's also possible
to define the number base using a string. Each character in the string
represents a digit and the position of each character represents it's value.
The base 16, for example, could be defined as 0123456789ABCDEF
. Defining
number bases this way also makes it easier to get resulting numbers in a
specific case. For example:
There is also a third way to define the number bases using an array. This allows even greater customization in terms of number bases. Each value in the array represents a digit and the index indicates the value. For example:
Credits
This library is Copyright (c) 2013-2017 Riikka Kalliomäki.
See LICENSE for license and copying information.
All versions of kit-baseconversion with dependencies
ext-gmp Version *