{"id":22104689,"url":"https://github.com/riimu/kit-baseconversion","last_synced_at":"2025-07-25T02:31:37.070Z","repository":{"id":7166022,"uuid":"8466487","full_name":"Riimu/Kit-BaseConversion","owner":"Riimu","description":"Arbitrary precision number base conversion library","archived":false,"fork":false,"pushed_at":"2017-07-15T13:48:40.000Z","size":208,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-07-01T03:48:32.877Z","etag":null,"topics":["base-conversion","convert-numbers","math","php","php-library"],"latest_commit_sha":null,"homepage":"http://kit.riimu.net","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Riimu.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-02-27T22:00:09.000Z","updated_at":"2024-01-16T02:05:39.000Z","dependencies_parsed_at":"2022-09-15T09:21:14.892Z","dependency_job_id":null,"html_url":"https://github.com/Riimu/Kit-BaseConversion","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/Riimu/Kit-BaseConversion","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-BaseConversion","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-BaseConversion/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-BaseConversion/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-BaseConversion/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Riimu","download_url":"https://codeload.github.com/Riimu/Kit-BaseConversion/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Riimu%2FKit-BaseConversion/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266944270,"owners_count":24010485,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-25T02:00:09.625Z","response_time":70,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["base-conversion","convert-numbers","math","php","php-library"],"created_at":"2024-12-01T06:34:10.493Z","updated_at":"2025-07-25T02:31:36.804Z","avatar_url":"https://github.com/Riimu.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Arbitrary Precision Base Converter #\n\n*BaseConversion* is a PHP library for converting number bases, similar to PHP's\nbuilt in function `base_convert()`. However, unlike the built in function, this\nlibrary is not limited by 32 bit integers and is capable of converting numbers\nof arbitrary precision. This library also supports conversion of fractions and\nallows more customization in terms of number bases.\n\nIn order to optimize the conversion of large numbers, this library also employs\ntwo different conversion strategies. In some cases, it's possible to convert\nnumbers simply by replacing the digits with digits from the other base (e.g.\nwhen converting from base 2 to base 16). This is considerably faster than\nthe other strategy, which simply calculates the new number using arbitrary\nprecision integer arithmetic.\n\nThe API documentation, which can be generated using Apigen, can be read online\nat: http://kit.riimu.net/api/baseconversion/\n\n[![Travis](https://img.shields.io/travis/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://travis-ci.org/Riimu/Kit-BaseConversion)\n[![Scrutinizer](https://img.shields.io/scrutinizer/g/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-BaseConversion/)\n[![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/Riimu/Kit-BaseConversion.svg?style=flat-square)](https://scrutinizer-ci.com/g/Riimu/Kit-BaseConversion/)\n[![Packagist](https://img.shields.io/packagist/v/riimu/kit-baseconversion.svg?style=flat-square)](https://packagist.org/packages/riimu/kit-baseconversion)\n\n## Requirements ##\n\n  * The minimum supported PHP version is 5.6\n  * The library depends on the following PHP Extensions\n    * [`gmp`](http://php.net/manual/en/book.gmp.php) (only required IDN support)\n\n## Installation ##\n\n### Installation with Composer ###\n\nThe easiest way to install this library is to use Composer to handle your\ndependencies. In order to install this library via Composer, simply follow\nthese two steps:\n\n  1. Acquire the `composer.phar` by running the Composer\n     [Command-line installation](https://getcomposer.org/download/)\n     in your project root.\n\n  2. Once you have run the installation script, you should have the `composer.phar`\n     file in you project root and you can run the following command:\n\n     ```\n     php composer.phar require \"riimu/kit-baseconversion:^1.2\"\n     ```\n\nAfter installing this library via Composer, you can load the library by\nincluding the `vendor/autoload.php` file that was generated by Composer during\nthe installation.\n\n### Adding the library as a dependency ###\n\nIf you are already familiar with how to use Composer, you may alternatively add\nthe library as a dependency by adding the following `composer.json` file to your\nproject and running the `composer install` command:\n\n```json\n{\n    \"require\": {\n        \"riimu/kit-baseconversion\": \"^1.2\"\n    }\n}\n```\n\n### Manual installation ###\n\nIf you do not wish to use Composer to load the library, you may also download\nthe library manually by downloading the [latest release](https://github.com/Riimu/Kit-BaseConversion/releases/latest)\nand extracting the `src` folder to your project. You may then include the\nprovided `src/autoload.php` file to load the library classes.\n\n## Usage ##\n\nThe most convenient way to use this library is via the `baseConvert()` static\nmethod provided by the `BaseConverter` class. In most cases, it works the same\nway as `base_convert()` does. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\necho BaseConverter::baseConvert('A37334', 16, 2); // outputs: 101000110111001100110100\n```\n\nThe method accepts negative numbers and fractions in the same way. An optional\nfourth parameter can be used to define the precision for the conversion. For\nexample:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('-1BCC7.A', 16, 10)  . PHP_EOL; // outputs: -113863.625\necho BaseConverter::baseConvert('-1BCC7.A', 16, 10, 1); // outputs: -113863.6\n```\n\nThe static method is simply a convenient wrapper for creating an instance of\n`BaseConvert` and calling the `setPrecision()` and `convert()` methods. If you\nneed to convert multiple numbers, it's more efficient to call the object in a\nnon static manner. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\n$converter = new BaseConverter(16, 10);\n\necho $converter-\u003econvert('A37334') . PHP_EOL; // outputs: 10711860\necho $converter-\u003econvert('-1BCC7.A')  . PHP_EOL; // outputs: -113863.625\n\n$converter-\u003esetPrecision(1);\necho $converter-\u003econvert('-1BCC7.A'); // outputs: -113863.6\n```\n\nIf the provided number contains invalid digits that are not part of the defined\nnumber base, the method will return false instead.\n\n### Converting Fractions ###\n\nWhile this library does support conversion of fractions, it's important to\nunderstand that fractions cannot always be converted accurately from number base\nto another the same way that integers can be converted. This is result of the\nfact that not all fractions can be represented in another number base.\n\nFor example, let's say we have the number 0.1 in base 3. This equals the same\nas 1/3 in base 10. However, if you were to represent 1/3 as a decimal number,\nyou would get an infinitely repeating '0.3333...'. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('0.1', 3, 10)  . PHP_EOL; // outputs: 0.33\necho BaseConverter::baseConvert('0.1', 3, 10, 6)  . PHP_EOL; // outputs: 0.333333\necho BaseConverter::baseConvert('0.1', 3, 10, 12); // outputs: 0.333333333333\n```\n\nDue to this behavior, it is possible to set the precision used for inaccurate\nfraction conversions. As can be seen in the previous example, the precision\nvalue defines the maximum number of digits in the resulting number. The result\nmay have less digits, however, if the number can be accurately converted using\na small number of digits. The precision may also be completely ignored, if the\nconverter knows, that it can accurately convert the fractions.\n\nThe precision value also has an alternative definition. If the precision is\n0 or a negative number, then the maximum number of digits in the resulting\nnumber is based on the precision of the original number. If the precision is 0,\nthe resulting number will have as many digits as it takes to represent the\nnumber in the same precision as the original number. A negative number will\nsimply increase the number of digits in addition to that. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('0.A7', 16, 10, 0)  . PHP_EOL; // outputs: 0.652\necho BaseConverter::baseConvert('0.A7', 16, 10, -2); // outputs: 0.65234\n```\n\nIn the previous example, the original number is `0.A7` in the base 16. A base 16\nnumber with two digits in the fractional part can represent a number up to\naccuracy of `1/(16 * 16) == 1/256`. To represent the the fractional part in the\nsame accuracy in base 10, we need at least 3 digits, because two digit can only\nrepresent numbers up to accuracy of `1/100`.\n\nThe default precision value used by the library is `-1`. It is also important\nto note that the last digit is not rounded (due to the fact that it would\ncause inconsistent results in some cases).\n\n### Case Sensitivity ###\n\nIn order to make user interaction with the library more convenient, the library\ntreats all numbers in a case insensitive manner, unless the number base\nprohibits that. For example, the base 16 can be treated in a case insensitive\nmanner, because it only defines the value for the digits `0-9A-F`. However,\nbase 62 cannot be treated in a case insensitive manner, because letters like\n`A` and `a` have a different value.\n\nThe returned numbers will always respect the character case defined by the\nnumber base. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('7BFA11', 16, 12)  . PHP_EOL; // outputs: 2879B29\necho BaseConverter::baseConvert('7bfa11', 16, 12); // outputs: 2879B29\n```\n\n### Customizing Number Bases ###\n\nOne of the features of this library is that allows much better customization of\nnumber bases than `base_convert()`. In most cases, you will probably define the\nnumber base using a simple integer such as `10` or `16`. However, there is no\nlimit to the size of that integer. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('7F', 16, 1024)  . PHP_EOL; // outputs: #0127\necho BaseConverter::baseConvert('5Glm1z', 64, 512); // outputs: #456#421#310#371\n```\n\nFor large number bases, however, the digits are simply represented by a string\nthat consists of `#` and the value for the digit. Whenever the number base is\ndefined using an integer, the digits follow the following rules:\n\n  * Bases equal or smaller than 62 use digits from the string\n    `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`\n  * A base 64 number uses digits from the base64 standard, i.e.\n    `ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/`\n  * Other bases equal or smaller than 256 use bytes as digits with byte value\n    indicating the digit value.\n  * Large bases use strings for digits that consist of `#` and the value for the\n    digit (the length of the string depends on the greatest digit value).\n    \nIn addition to defining the number base using an integer, it's also possible\nto define the number base using a string. Each character in the string\nrepresents a digit and the position of each character represents it's value.\nThe base 16, for example, could be defined as `0123456789ABCDEF`. Defining\nnumber bases this way also makes it easier to get resulting numbers in a\nspecific case. For example:\n\n```php\n\u003c?php\n\nrequire 'vendor/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('101100011101', '01', 16)  . PHP_EOL; // outputs: B1D\necho BaseConverter::baseConvert('101100011101', 2, '0123456789abcdef'); // outputs: b1d\n```\n\nThere is also a third way to define the number bases using an array. This allows\neven greater customization in terms of number bases. Each value in the array\nrepresents a digit and the index indicates the value. For example:\n\n```php\n\u003c?php\n\nrequire 'src/autoload.php';\nuse Riimu\\Kit\\BaseConversion\\BaseConverter;\n\necho BaseConverter::baseConvert('22', 10, ['nil', 'one'])  . PHP_EOL; // outputs: oneniloneonenil\necho BaseConverter::baseConvert('187556', 10, ['-', '-!', '-\"', '-#', '-¤', '-%']); // outputs: -¤---¤-!-%-\"\n```\n\n## Credits ##\n\nThis library is Copyright (c) 2013-2017 Riikka Kalliomäki.\n\nSee LICENSE for license and copying information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-baseconversion","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Friimu%2Fkit-baseconversion","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Friimu%2Fkit-baseconversion/lists"}