BaseConvert helper


1Presentation

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


2Usage

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 950123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#"',;\_`|~

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

  • base 85: All printable characters usable on the command line without escapement.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$#
  • base 84: Derived from base 65, from which the colon character (:) has been removed, because it is often used to separate logins and passwords.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-+=^!/*?&<>()[]{}@%$#
  • base 80: Derived from base 85, from which HTML special characters (&<>?/) have been removed.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!*()[]{}@%$#
  • base 79: Derived from base 80, from which the colon character (:) has been removed, because it is often used to separate logins and passwords.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-+=^!*()[]{}@%$#
  • base 73: Derived from base 54, with additional special characters.
    23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ!#%()*+,-./:;=?@[]_
  • base 71: Contains all characters not encoded in a URL.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!'()*-._~
  • base 70: Derived from base 71 (all characters not encoded in a URL), without the quote character (').
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!()*-._~
  • base 64: Derived from base 62 (0-9a-zA-Z), with the '+' and '/' characters.
    0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/
  • base 61: Derived from base 71, without similar characters (0oO1ilIL!i~).
    23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ'()*-._
  • base 60: Derived from base 61, without the quote character (').
    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

3convert()

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);

4convertBase()

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');

5convertFromSpecialBase()

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);

6convertToSpecialBase()

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);
© 2007-2024 All Rights Reserved.