Download the PHP package delight-im/base-convert without Composer
On this page you can find all versions of the php package delight-im/base-convert. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download delight-im/base-convert
More information about delight-im/base-convert
Files in delight-im/base-convert
Package base-convert
Short Description Conversion of arbitrarily large numbers between any two bases or alphabets
License MIT
Homepage https://github.com/delight-im/PHP-BaseConvert
Informations about the package base-convert
PHP-BaseConvert
Conversion of arbitrarily large numbers between any two bases or alphabets
Requirements
- PHP 5.6.0+
- GMP (GNU Multiple Precision) extension (
gmp
)
- GMP (GNU Multiple Precision) extension (
Installation
-
Include the library via Composer [?]:
- Include the Composer autoloader:
Usage
Conversions between bases
Note: Only bases between 2 (inclusive) and 62 (inclusive) are supported via Base::convert
. For larger bases, you must use Alphabet::convert
instead and specify the alphabet that should be used explicitly.
Conversions between alphabets
Available alphabets
When passing an alphabet to Alphabet::convert
as either the source or the target alphabet, you can use any alphabet that exclusively consists of ASCII or single-byte UTF-8 characters. Of course, no single character may appear more than once in a given alphabet.
Alternatively, you can use any of the following pre-defined alphabets that should be sufficient for many purposes:
Built-in PHP functions
If you prefer a consistent interface for bases of any size, you can replace PHP’s built-in functions for base conversions as follows:
\bindec($n)
can be replaced with …\Delight\BaseConvert\Base::convert($n, 2, 10)
\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::BINARY, \Delight\BaseConvert\Alphabet::DECIMAL)
\octdec($n)
can be replaced with …\Delight\BaseConvert\Base::convert($n, 8, 10)
\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::OCTAL, \Delight\BaseConvert\Alphabet::DECIMAL)
\decbin($n)
can be replaced with …\Delight\BaseConvert\Base::convert($n, 10, 2)
\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::DECIMAL, \Delight\BaseConvert\Alphabet::BINARY)
\decoct($n)
can be replaced with …\Delight\BaseConvert\Base::convert($n, 10, 8)
\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::DECIMAL, \Delight\BaseConvert\Alphabet::OCTAL)
\dechex($n)
can be replaced with …\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::DECIMAL, \Delight\BaseConvert\Alphabet::HEX_LOWERCASE)
\Delight\BaseConvert\Base::convert($n, 10, 16)
(uppercase)
\hexdec($n)
can be replaced with …\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::HEX, \Delight\BaseConvert\Alphabet::DECIMAL)
\Delight\BaseConvert\Base::convert($n, 16, 10)
\hex2bin($n)
can be replaced with …\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::HEX_LOWERCASE, \Delight\BaseConvert\Alphabet::BYTE)
\bin2hex($n)
can be replaced with …\Delight\BaseConvert\Alphabet::convert($n, \Delight\BaseConvert\Alphabet::BYTE, \Delight\BaseConvert\Alphabet::HEX_LOWERCASE)
base_convert($n, $fromBase, $toBase)
can be replaced with …\Delight\BaseConvert\Base::convert($n, $fromBase, $toBase)
Contributing
All contributions are welcome! If you wish to contribute, please create an issue first so that your feature, problem or question can be discussed.
License
This project is licensed under the terms of the MIT License.