Documentation

BaseConvert helper

Table of Contents 

Presentation

This helper is used to convert databases. This can be especially useful for creating cryptographic tokens.

Usage

The \Temma\Utils\BaseConvert object offers several static methods for converting numbers from one numerical base to another.

Some methods wait for the base composition to be supplied. For example, for a classic hexadecimal base (base 16), this is the string 0123456789abcdef.

Other methods just need the base size. The composition of the base itself will then be a subset of base 95, which is a base containing all 95 printable ASCII characters: 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"',;\_`|~

Finally, other methods use "special" bases, which are specific subsets:

  • base 85: All printable characters usable on the command line without escapement.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#
  • base 80: Derived from base 85, from which special characters have been removed in HTML (&<>?/).
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!*()[]{}@%$#
  • base 73: Derived from base 54, with additional special characters.
    23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!#%()*+,-./:;=?@[]_
  • base 71: Contains all characters not encoded in a URL.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'()*-._~
  • base 61: Derived from base 71, without similar characters (0oO1ilIL!i~).
    23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ'()*-._
  • base 54: Derived from base 62 (0-9a-zA-Z), without smilar characters (0oO1iIlL).
    23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ
  • base 31: Derived from base 36 (0-9a-z), without similar characters (0o1il).
    23456789abcdefghjkmnpqrstuvwxyz

convert()

Method signature:

convert(string $input, int $inBase, int $outBase) : string

Parameters:

  1. $input: String containing the number to convert.
  2. $inBase: Input base size (<= 95).
  3. $outBase: Output base size (<= 95).

This method converts a number from any base to any base. The sizes of the input and output bases are given as parameters; these bases are therefore subsets of base 95.

Examples:

use \Temma\Utils\BaseConvert as TµBaseConvert;

// converts "255" (base 10) into "ff" (base 16)
$res = TµBaseConvert::convert('255', 10, 16);

// converts "99999" (base 10) into "b7X" (base 95)
$res = TµBaseConvert::convert('99999', 10, 95);

convertBase()

Method signature:

convertBase(int|string $value, string $inDigits, string $outDigits) : string

Parameters:

  1. $value: Value to be converted.
  2. $inDigits: String containing the input base.
  3. $outDigits: String containing the output base.

This method converts a number from any base to any base. The characters making up the input and output bases are given as parameters.

Example:

use \Temma\Utils\BaseConvert as TµBaseConvert;

// converts the number "99" (base 10) into "iuo"
// (in the vowel base)
$res = TµBaseConvert::convertBase(99, '0123456789', 'aeiouy');

convertFromSpecialBase()

Method signature:

convertFromSpecialBase(string $input, int $inBase, int $outBase) : string

Parameters:

  1. $input: Value to be converted.
  2. $inBase: Input base size (31, 54, 61, 71, 73, 80, 85, 95).
  3. $outBase: Output base size (<= 95).

This method converts a number from a special base (see list above), to any base subset of base 95.

Example:

use \Temma\Utils\BaseConvert as TµBaseConvert;

// converts the number "5d3t" (base 31)
// into "99999" (base 10)
$res = TµBaseConvert::convertFromSpecialBase('5d3t', 31, 10);

convertToSpecialBase()

Method signature:

convertToSpecialBase(string $input, int $inBase, int $outBase) : string

Parameters:

  1. $input: Value to be converted.
  2. $inBase: Input base size (<= 95).
  3. $outBase: Output base size (31, 54, 61, 71, 73, 80, 85, 95).

This method converts a number from any base (subset of base 95) to a special base (see list above).

Example:

use \Temma\Utils\BaseConvert as TµBaseConvert;

// converts the number "99999" (base 10)
// into "5d3t" (base 31)
$res = TµBaseConvert::convertToSpecialBase(99999, 10, 31);
Previous: ANSI helper
Next: DataFilter helper

Table of Contents