Arbitrary number base converter.

Overview

Numbase

Build License Version Psalm

Easily convert numbers between arbitrary bases and symbol sets.

Installation

This library is available on Packagist as thunderer/numbase. It requires PHP >=7.4 and GMP extension for handling large numbers. For older PHP versions please use ^0.1 constraint which works with 5.3 and above.

Usage

The Simplest Way™

use Thunder\Numbase\Numbase;

$numbase = Numbase::createDefault();

// decimal 15 to hexadecimal number
assert('F' === $numbase->convert(15, 10, 16));
// 64000 decimal to base 32000
assert('20' === $numbase->convert(64000, 10, 32000));

Regular usage (see Internals section for more options):

use Thunder\Numbase\Numbase;

$base62 = new Base62Symbols();
$numbase = new Numbase(new GmpConverter($base62), new StrictFormatter($base62));

// decimal 15 to hexadecimal number
assert('F' === $numbase->convert(15, 10, 16));

Showcase

Convert number to and from a different set of symbols:

$base10 = new Base10Symbols();
$upper = new StringSymbols('[email protected]#$%^&*()');

$numbase = new Numbase(new GmpDigits($base10), new StrictFormatter($upper));

assert('#!' === $numbase->convert('20', 10, 10));
assert('-$!' === $numbase->convert('-30', 10, 10));

$numbase = new Numbase(new GmpDigits($upper), new StrictFormatter($base10));

assert('20' === $numbase->convert('#!', 10, 10));
assert('-30' === $numbase->convert('-$!', 10, 10));

Get array of digit values (for bases too large for any symbol set):

$numbase = new Numbase(new GmpDigits(new Base62Symbols()), new ArrayFormatter());

// convert 10^12 to base 99:
assert(array('10', '61', '53', '3', '51', '60', '10') 
    === $numbase->convert('10000000000000', 10, 99));

Internals

Numbase is built upon several concepts:

  • converters that convert numbers to array of numbers of digits,
  • formatters that take those arrays and return final numbers,
  • symbols used in converters to check symbols values and to get digits symbols in formatters.

There are several implementations of each concept bundled with this library, for example:

  • converters:
    • GmpConverter: can convert any integer between any base greater than 2, uses gmp_*() functions,
    • GmpStrvalConverter: uses gmp_strval() to convert between bases 2 and 62,
    • BaseConvertConverter: uses base_convert() to convert between bases 2 and 32,
  • formatters:
    • ArrayFormatter: returns raw array of digits numbers,
    • StrictFormatter: returns number as string, throws exception when digit is not found in symbols set,
    • FallbackFormatter: returns number as string, but returns string with digit values separated by configured separator when any digit is not found in symbols set,
  • symbols:
    • ArraySymbols: takes associative array(value => symbol),
    • Base62Symbols: contains alphanumeric set of symbols 0-9A-Za-z up to base 62,
    • StringSymbols: takes string and splits it assigning consecutive values to each character.

The named constructor Numbase::createDefault() uses GmpConverter, StrictFormatter and Base62Symbols as defaults.

License

See LICENSE file in the main directory of this library.

You might also like...
Smd thumbnail - Multiple image thumbnails of arbitrary dimensions

smd_thumbnail Download | Packagist If you’re bored of one Textpattern thumbnail per image and don’t fancy using an auto-resizing script or relying on

A filesystem-like repository for storing arbitrary resources.

The Puli Repository Component Latest release: 1.0.0-beta10 PHP = 5.3.9 The Puli Repository Component provides an API for storing arbitrary resources

Base85 encoder and decoder for arbitrary data

All your Base85 Install Install with composer. $ composer require tuupola/base85 This branch requires PHP 7.1 or up. The older 1.x branch supports als

Base62 encoder and decoder for arbitrary data

Base62 This library implements base62 encoding. In addition to integers it can encode and decode any arbitrary data. This is useful for example when g

PHP Class converter to namepaces.

Namespacer This tool will assist you in taking older underscore/prefix spaced code and will namespace it as best as it can, and also make the files an

HTML to PDF converter for PHP

Dompdf Dompdf is an HTML to PDF converter At its heart, dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is

An ANSI to HTML5 converter

ANSI to HTML5 Converter This small library only does one thing: converting a text containing ANSI codes to an HTML5 fragment: require_once __DIR__.'/v

Laravel: Data to Monthly Converter

Laravel: Data to Monthly Converter Using this package you will be able to converts your collection using a timestamp field to order data monthly or ye

Unit converter and calculator for php

Unit converter and calculator This library uses the awesome lisachenko/z-engine to allow mathematical operations on objects, allowing to do stuff like

UUID = ULID bidirectional converter

uuid-ulid-converter UUID = ULID bidirectional converter Installing composer require mpyw/uuid-ulid-converter API static string Converter::uuidToUli

Google-like values converter

Google-like values converter. Support for different types of conversions, for examples: 1 kilometer - meters 1 dollar - THB 1 kilogram - meters ...

PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!
A full-featured home hosted Cloud Drive, Personal Assistant, App Launcher, File Converter, Streamer, Share Tool & More!

A Fully Featured home-hosted Cloud Storage platform and Personal Assistant that Converts files, OCR's images & documents, Creates archives, Scans for viruses, Protects your server, Keeps itself up-to-date, and Runs your own AppLauncher!

JavaScript to JQuery Converter

JQuery To Javascript Converter for helping to removing JQuery on websites.

The WordPress Categories to Tags Converter.

=== Categories to Tags Converter === Contributors: wordpressdotorg Donate link: Tags: importer, categories and tags converter Requires at least: 3.0

Small PHP library to valid email addresses using a number of methods.

Email Validator Small PHP library to valid email addresses using a number of methods. Features Validates email address Checks for example domains (e.g

PHP version of Google's phone number handling library

libphonenumber for PHP What is it? A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on

Phone number functionality for Laravel

Laravel Phone Adds phone number functionality to Laravel and Lumen based on the PHP port of Google's libphonenumber API by giggsey. Table of Contents

Magento 2 module to only allow checkout when the number of items in the cart are a multiple of X.

Cart Quantity Multiple - Magento 2 Module Introduction This module allows to limit checkout only when the contents of the cart are a multiple of X

Comments
  • Test enhancement

    Test enhancement

    Changed log

    • The php-5.3 test needs the precise dist in Travis CI build.
    • To be compatible with the different PHPUnit version, using the expectedException annotation.
    • Set the different PHPUnit version to support the multiple PHP versions.
    opened by peter279k 8
  • The future is now!

    The future is now!

    • [x] migrated from Travis CI to Github Actions,
    • [x] introduced Makefile,
    • [x] removed legacy badges, only build status, license and version remain,
    • [x] bumped minimal PHP version to 7.4,
    • [x] added support for PHP 8.0,
    • [x] introduced Psalm with 100% type coverage and no reported issues,
    • [x] introduced Docker Compose with dedicated Dockerfile,
    • [x] updated PHPUnit XML file,
    • [x] updated whole codebase to leverage PHP features up to 7.4 (previously 5.6).
    opened by thunderer 0
Releases(v0.1.2)
Owner
Tomasz Kowalczyk
Working hard to bring back *engineering* in software engineering.
Tomasz Kowalczyk
Unit converter and calculator for php

Unit converter and calculator This library uses the awesome lisachenko/z-engine to allow mathematical operations on objects, allowing to do stuff like

Dominik Chrástecký 8 Apr 8, 2022
PHP version of Google's phone number handling library

libphonenumber for PHP What is it? A PHP library for parsing, formatting, storing and validating international phone numbers. This library is based on

Joshua Gigg 4.2k Dec 5, 2022
This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify that a number is indeed a number of an operator X

phone-number-checker This package is used to validate the telephone numbers of the countries taken into account. It also makes it possible to verify t

faso-dev 4 Feb 7, 2022
A filesystem-like repository for storing arbitrary resources.

The Puli Repository Component Latest release: 1.0.0-beta10 PHP >= 5.3.9 The Puli Repository Component provides an API for storing arbitrary resources

Puli 435 Nov 20, 2022
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function that you can use instead of var_dump().

VarDumper Component The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function t

Symfony 7.1k Dec 2, 2022
The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function that you can use instead of var_dump().

VarDumper Component The VarDumper component provides mechanisms for walking through any arbitrary PHP variable. It provides a better dump() function t

Symfony 7.1k Dec 7, 2022
Arbitrary-precision arithmetic library for PHP

Arbitrary-precision arithmetic library for PHP

Brick 1.4k Nov 26, 2022
Okex API Like the official document interface, Support for arbitrary extension.

It is recommended that you read the official document first Okex docs https://www.okex.com/docs/en Okex Simulation Test API https://www.okex.com/docs/

lin 34 Oct 29, 2022
Exploit the vulnerability to install arbitrary applications in k61v1 without ROOT

k61v1injector Arbitrary application installer for Qin F21 Pro Exploit the vulnerability to install arbitrary applications in k61v1 without ROOT. Feel

Jim Wu 14 Nov 10, 2022
Checking an arbitrary URL for Micro-Framework HLEB

Checking an arbitrary URL for Micro-Framework HLEB The class RouteFinder is not included in the original configuration of the framework HLEB, so it mu

Foma Tuturov 1 Nov 4, 2021