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:
- $input: String containing the number to convert.
- $inBase: Input base size (<= 95).
- $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:
- $value: Value to be converted.
- $inDigits: String containing the input base.
- $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:
- $input: Value to be converted.
- $inBase: Input base size (31, 54, 61, 71, 73, 80, 85, 95).
- $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:
- $input: Value to be converted.
- $inBase: Input base size (<= 95).
- $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
- Migration : How to upgrade from Temma 1.x to version 2
- Installation : Download Temma and install it to start your web project
- Configuration : All the configuration directives of the etc/temma.json file and the optional environment variables
- External libraries : How to use external function libraries
- Routing : Temma's default routing system, and advanced routing
- Log : Use of the log system, management by criticality levels
- Controllers : Essential parts of your web application
- Views : Smarty templates or JSON/CSV/RSS/iCal/INI exports
- Dependency injection : The backbone of your application developments
- Sessions : Extension of user sessions managed by PHP
- Data sources : Unified data access management
- Model : How to use DAOs to access databases
- Execution flow : How to manage the execution flow, between plugins and the controller
- Plugins : How to use plugins, and create your own plugins to modularize your code
- Attributes : How to filter access to controllers and actions
- Command-line interface : To create executable scripts on the command line, automatically initialized by Temma
-
Helpers :
Items offered by Temma to help you in various circumstances
- Command-line scripts
-
Controller + plugin
- Auth : Controller and plugin to manage user authentication
- Plugins
- Attributes
-
Utility objects
- ANSI : To format texts written to standard output
- BaseConvert : To do digital base conversions
- DataFilter : To filter and validate data
- Email : To send emails
- HTMLCleaner : To clean up an HTML stream coming from a WYSIWYG editor
- IniExport : To export data in INI format
- Json : To read JSON streams that may contain comments
- Lock : To lock access to a file, or the execution of the current PHP script
- Registry : To properly store global variables
- Smarty : To process Smarty templates outside the view
- Term : Terminal management (TTY)
- Text : Different treatments on character strings
- Timer : To manage stopwatches