https://github.com/artbit/base_convert
Custom PHP base_convert method that supports bases from 2 to 64
https://github.com/artbit/base_convert
base-conversion number-converter php
Last synced: 10 months ago
JSON representation
Custom PHP base_convert method that supports bases from 2 to 64
- Host: GitHub
- URL: https://github.com/artbit/base_convert
- Owner: ArtBIT
- License: other
- Created: 2015-10-09T16:40:58.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2021-09-06T14:10:46.000Z (almost 5 years ago)
- Last Synced: 2025-08-23T21:14:40.688Z (10 months ago)
- Topics: base-conversion, number-converter, php
- Language: PHP
- Size: 21.5 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Base Convert [2..64]
[](https://travis-ci.org/ArtBIT/base_convert) [](https://github.com/ArtBIT/base_convert) [](https://github.com/ArtBIT/base_convert) [](https://github.com/ArtBIT/base_convert)
PHP's built in `base_convert` function supports bases ranging from 2 to 36. This library expands that range to 2 to 64.
# Usage
Convert a number 100 from decimal to hexadecimal (from base 10 to base 16):
```php
echo math\base_convert(100, 10, 16);
// echoes '64'
```
...and back:
```php
echo math\base_convert(64, 16, 10);
// echoes '100'
```
## Custom alphabets
Instead of integer bases, you can pass in the alphabet string to use for conversion (since integer bases are converted to alphabet strings anyways, i.e. hexadecimal alphabet is simply "0123456789abcdef").
Here we convert from base 10 to alphabet 'customizable'
```php
echo math\base_convert(1234567890, 10, 'customizable');
// echoes 'slmmmmcui'
```
Here we convert from alphabet 'customizable' to alphabet 'isogram'
```php
echo math\base_convert('slmmmmcui', 'customizable', 'isogram');
// echoes 'rorsirrioig'
```
And from alphabet 'isogram' back to base 10
```php
echo math\base_convert('rorsirrioig', 'isogram', 10);
// echoes '1234567890'
```
So both `slmmmmcui` and `rorsirrioig`, but also `1234567890` describe the same value, but in different alphabets.
*NOTE:* All alphabets must be [isograms](https://en.wikipedia.org/wiki/Isogram)
An isogram (also known as a "nonpattern word") is a logological term for a word or phrase without a repeating letter. Conveniently, the word `isogram` is an isogram as well.
# License
[MIT](LICENSE)