MODASCII / MODULAR ARITHMETIC

A string. A number. A remainder.

Turn your words into exact modular arithmetic.

How the numbering works

The alphabet contains all 128 ASCII characters, ordered by their codes from 0 to 127. This includes control characters such as NUL, as well as printable characters. The single-line input is intended for printable ASCII text; it does not provide a way to type every control character.

Strings are numbered starting at 0: first the empty string, then every one-character string, then every two-character string, and so on. Within each length, strings are ordered lexicographically by ASCII code (comparing from left to right).

Start with ID = 0. Read the input from left to right and, for each character, update ID = 128 × ID + ASCII code + 1. The added 1 accounts for the positions occupied by shorter strings. For length L, this is the ordinary base-128 value of the ASCII codes plus 1 + 128 + … + 128^(L−1); for the empty string, both terms are 0.

The empty string has ID 0. Single-character IDs run from 1 (NUL, code 0) to 128 (DEL, code 127). Two-character IDs run from 129 to 16,512. Thus the numbering is consecutive and gives each distinct ASCII string a unique ID. Character order, letter case and spaces all matter.

Example: A has ASCII code 65, so its ID is 128 × 0 + 65 + 1 = 66. Appending B (code 66) gives AB → 128 × 66 + 66 + 1 = 8,515. With the default modulus: 8,515 mod 713 = 672.

The result is the remainder after dividing the ID by the positive integer n, so 0 ≤ result < n. Different strings can have the same remainder, even though their IDs differ. This app accepts up to 256 ASCII characters and a positive modulus entered using at most 100 decimal digits; the default is 713. PHP performs exact decimal arithmetic without additional extensions.