Base converting: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | The following [[Unit|unit]] will allow you to convert from one number base to another. Each base is chosen in the range `2..36`. | |
| 2 | 2 | ||
| 3 | 3 | Note, the unit does not know a negative sign. | |
| 4 | 4 | ||
| 5 | 5 | ## Source | |
| 6 | 6 | ||
| 7 | 7 | ``` pascal | |
| 8 | 8 | unit baseConvert; | |
| 9 | 9 | ||
| 10 | 10 | // for exceptions | |
| 11 | 11 | {$mode objfpc} | |
| 12 | 12 | ||
| 13 | 13 | // P U B L I C ======================================================== // | |
| 14 | 14 | interface | |
| 15 | 15 | ||
| 16 | 16 | const | |
| 17 | 17 | /// list of symbols for integer representations | |
| 18 | 18 | digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
| 19 | 19 | ||
| 20 | 20 | function intToBaseStr(value: longword; const base: cardinal = 10): string; | |
| 21 | 21 | function baseStrToInt(const value: string; const base: cardinal = 10): longword; | |
| 22 | 22 | ||
| 23 | 23 | function baseToBase( | |
| 24 | 24 | const value: string; | |
| 25 | 25 | const interpretation: cardinal = 10; | |
| 26 | 26 | const base: cardinal = 10 | |
| 27 | 27 | ): string; | |
| 28 | 28 | ||
| 29 | 29 | ||
| 30 | 30 | // P R I V A T E ====================================================== // | |
| 31 | 31 | implementation | |
| 32 | 32 | ||
| 33 | 33 | uses | |
| 34 | 34 | // math unit for inRange and divMod routines | |
| 35 | 35 | math, | |
| 36 | 36 | // sysutils unit for ERangeError exception | |
| 37 | 37 | sysutils; | |
| 38 | 38 | ||
| 39 | 39 | resourceString | |
| 40 | 40 | /// base not within range of available symbols | |
| 41 | 41 | errInvalidBase = '↯ b = %0:d! ⇒ b ∉ [%1:d, %2:d]'; | |
| 42 | 42 | /// supplied character not within domain | |
| 43 | 43 | errInvalidSymbol = '↯ v(''%0:s'')↑; b = %1:d'; | |
| 44 | 44 | ||
| 45 | 45 | (** | |
| 46 | 46 | \brief expresses the value in the desired base | |
| 47 | 47 | ||
| 48 | 48 | \param value the value to convert | |
| 49 | 49 | \param base | |
| 50 | 50 | ||
| 51 | 51 | \return a string representing the number in given base | |
| 52 | 52 | *) | |
| 53 | 53 | function intToBaseStr(value: longword; const base: cardinal = 10): string; | |
| 54 | 54 | var | |
| 55 | 55 | remainder: longword; | |
| 56 | 56 | begin | |
| 57 | 57 | // validity of the base | |
| 58 | 58 | if not inRange(base, 2, length(digits)) then | |
| 59 | 59 | begin | |
| 60 | 60 | raise eRangeError.createFmt(errInvalidBase, [base, 2, length(digits)]); | |
| 61 | 61 | end; | |
| 62 | 62 | ||
| 63 | 63 | // decomposing the value: reverse Horner-scheme | |
| 64 | 64 | // The repeat-statement takes the value 0 into account. | |
| 65 | 65 | result := emptyStr; | |
| 66 | 66 | repeat | |
| 67 | 67 | begin | |
| 68 | 68 | divMod(value, base, value, remainder); | |
| 69 | 69 | // prepend value: intermediate results are read backwards | |
| 70 | 70 | result := digits[remainder + 1] + result; | |
| 71 | 71 | end | |
| 72 | 72 | until value = 0; | |
| 73 | 73 | end; | |
| 74 | 74 | ||
| 75 | 75 | (** | |
| 76 | 76 | \brief transforms the string of a number | |
| 77 | 77 | expressed in the parameter base | |
| 78 | 78 | into the equivalent integer | |
| 79 | 79 | ||
| 80 | 80 | \param value the representation of the integer | |
| 81 | 81 | \param base assume string is in given base | |
| 82 | 82 | ||
| 83 | 83 | If the string is empty, the result is 0. | |
| 84 | 84 | ||
| 85 | 85 | Caution: | |
| 86 | 86 | No verification of integer overflows. | |
| 87 | 87 | Reasonable values have to be used. | |
| 88 | 88 | ||
| 89 | 89 | \return the integer | |
| 90 | 90 | *) | |
| 91 | 91 | function baseStrToInt(const value: string; const base: cardinal = 10): longword; | |
| 92 | 92 | var | |
| 93 | 93 | symbol: char; | |
| 94 | 94 | // beware, cardinal is _non-negative_ | |
| 95 | 95 | digit: cardinal; | |
| 96 | 96 | begin | |
| 97 | 97 | result := 0; | |
| 98 | 98 | // Horner-scheme | |
| 99 | 99 | for symbol in value do | |
| 100 | 100 | begin | |
| 101 | 101 | // determine ordinal value of current symbol | |
| 102 | 102 | digit := pos(symbol, digits); | |
| 103 | 103 | // If symbol wasn't found, pos returns 0. | |
| 104 | 104 | // Or possibly the symbol isn't known in the base. | |
| 105 | 105 | if not inRange(digit, 1, base) then | |
| 106 | 106 | begin | |
| 107 | 107 | raise eRangeError.createFmt(errInvalidSymbol, [symbol, base]); | |
| 108 | 108 | end; | |
| 109 | 109 | // e.g. the symbol '0' is the _first_ character in digits | |
| 110 | 110 | // thus the pos function returned 1 => here we subtract one | |
| 111 | 111 | dec(digit); | |
| 112 | 112 | result := result * base + digit; | |
| 113 | 113 | end; | |
| 114 | 114 | end; | |
| 115 | 115 | ||
| 116 | 116 | (** | |
| 117 | 117 | \brief direct conversion of number expressed in one base to another | |
| 118 | 118 | ||
| 119 | 119 | \param value the value to convert from | |
| 120 | 120 | \param interpretation the base to interpret the value as | |
| 121 | 121 | \param base the base to convert to | |
| 122 | 122 | *) | |
| 123 | 123 | function baseToBase( | |
| 124 | 124 | const value: string; | |
| 125 | 125 | const interpretation: cardinal = 10; | |
| 126 | 126 | const base: cardinal = 10 | |
| 127 | 127 | ): string; | |
| 128 | 128 | begin | |
| 129 | 129 | // the case interpretation = base | |
| 130 | 130 | // intentionally does not have special treatment for unit tests | |
| 131 | 131 | result := intToBaseStr(baseStrToInt(value, interpretation), base); | |
| 132 | 132 | end; | |
| 133 | 133 | ||
| 134 | 134 | // end of unit | |
| 135 | 135 | end. | |
| 136 | 136 | ``` | |
| 137 | 137 | ||
| 138 | 138 | ## Remarks | |
| 139 | 139 | ||
| 140 | 140 | For your information: For bases two, eight, ten, and sixteen, you can use `binStr`, `OctStr`, `str` and `hexStr` from the automatically included `system` unit. | |
| 141 | 141 | ||
| 142 | 142 | ## Locales | |
| 143 | 143 | ||
| 144 | - | ||
| 144 | + | `en.po` | |
| 145 | 145 | ||
| 146 | 146 | ``` bash | |
| 147 | 147 | #: baseconvert:errinvalidbase | |
| 148 | 148 | msgid "\342\206\257\302\240b\302\240= %0:d!\302\240\342\207\222 b\302\240\342\210\211\302\240[%1:d, %2:d]" | |
| 149 | 149 | msgstr "Base %0:d is invalid. It must be between %1:d and %2:d." | |
| 150 | 150 | ||
| 151 | 151 | #: baseconvert:errinvalidsymbol | |
| 152 | 152 | msgid "\342\206\257\302\240v('%0:s')\342\206\221; b\302\240= %1:d" | |
| 153 | 153 | msgstr "The symbol '%0:s' has an unacceptable value for base = %1:d." | |
| 154 | 154 | ``` | |
| 155 | 155 | ||
| 156 | - | ||
| 157 | - | ||
| 158 | - | ||
| 156 | + | `de.po` | |
| 159 | 157 | ||
| 160 | 158 | ``` bash | |
| 161 | 159 | #: baseconvert:errinvalidbase | |
| 162 | 160 | msgid "\342\206\257\302\240b\302\240= %0:d!\302\240\342\207\222 b\302\240\342\210\211\302\240[%1:d, %2:d]" | |
| 163 | 161 | msgstr "Die Basis %0:d ist ungültig. Sie muß zwischen %1:d und %2:d sein." | |
| 164 | 162 | ||
| 165 | 163 | #: baseconvert:errinvalidsymbol | |
| 166 | 164 | msgid "\342\206\257\302\240v('%0:s')\342\206\221; b\302\240= %1:d" | |
| 167 | 165 | msgstr "Der Zeichenwert von '%0:s' ist ungültig für Zahlen der Basis %1:d." | |
| 168 | 166 | ``` | |
| 169 | 167 | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 168 | + | `fr.po`: | |
| 173 | 169 | ||
| 174 | 170 | ``` bash | |
| 175 | 171 | #: baseconvert:errinvalidbase | |
| 176 | 172 | msgid "\342\206\257\302\240b\302\240= %0:d!\302\240\342\207\222 b\302\240\342\210\211\302\240[%1:d, %2:d]" | |
| 177 | 173 | msgstr "Base %0:d invalide, elle doit être comprise entre %1:d et %2:d." | |
| 178 | 174 | ||
| 179 | 175 | #: baseconvert:errinvalidsymbol | |
| 180 | 176 | msgid "\342\206\257\302\240v('%0:s')\342\206\221; b\302\240= %1:d" | |
| 181 | 177 | msgstr "Mauvais chiffre '%0:s' dans la valeur, base = %1:d." | |
| 182 | 178 | ``` | |
| 183 | - | ||
| 184 | - | ||
| 185 | 179 | ||
| 186 | 180 | ## More ... | |
| 187 | 181 | ||
| 188 | 182 | A more elaborate implementation (based upon the example code above) can be found in [this FsiConv unit](http://svn.code.sf.net/p/flyingsheep/code/trunk/MijnLib/fsiconv.pp).\ | |
| 189 | 183 | It has implementations for all integer types (signed and unsigned) as well as TryStrToXXX and StrToXXXDef functions. | |
| 190 | 184 | ||
| 191 | 185 | --- | |
| 192 | 186 | *From the Free Pascal wiki, page "Base converting", as it stood on 2022-05-26 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
