Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/riimu/kit-baseconversion

Arbitrary precision number base conversion library
https://github.com/riimu/kit-baseconversion

base-conversion convert-numbers math php php-library

Last synced: about 2 months ago
JSON representation

Arbitrary precision number base conversion library

Awesome Lists containing this project

README

        

# 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/

[![Travis](https://img.shields.io/travis/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://travis-ci.org/Riimu/Kit-BaseConversion)
[![Scrutinizer](https://img.shields.io/scrutinizer/g/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-BaseConversion/)
[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-BaseConversion/)
[![Packagist](https://img.shields.io/packagist/v/riimu/kit-baseconversion.svg?style=flat-square)](https://packagist.org/packages/riimu/kit-baseconversion)

## Requirements ##

* The minimum supported PHP version is 5.6
* The library depends on the following PHP Extensions
* [`gmp`](http://php.net/manual/en/book.gmp.php) (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:

1. Acquire the `composer.phar` by running the Composer
[Command-line installation](https://getcomposer.org/download/)
in your project root.

2. 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:

```
php composer.phar require "riimu/kit-baseconversion:^1.2"
```

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:

```json
{
"require": {
"riimu/kit-baseconversion": "^1.2"
}
}
```

### 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](https://github.com/Riimu/Kit-BaseConversion/releases/latest)
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:

```php
convert('A37334') . PHP_EOL; // outputs: 10711860
echo $converter->convert('-1BCC7.A') . PHP_EOL; // outputs: -113863.625

$converter->setPrecision(1);
echo $converter->convert('-1BCC7.A'); // outputs: -113863.6
```

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:

```php