Base converting/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Base converting/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | Cette unité vous permettra de passer d'une base de numération à une autre, chaque base est choisie dans l'intervalle 2..36. | |
| 2 | + | ||
| 3 | + | ``` pascal | |
| 4 | + | ||
| 5 | + | unit BaseConvert; | |
| 6 | + | ||
| 7 | + | interface | |
| 8 | + | ||
| 9 | + | function IntToBaseStr(Value, BaseOut: integer): String; | |
| 10 | + | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; | |
| 11 | + | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; | |
| 12 | + | ||
| 13 | + | implementation | |
| 14 | + | ||
| 15 | + | resourceString | |
| 16 | + | rsBadBase = 'Base %d invalide, elle doit être comprise entre' | |
| 17 | + | +' %d et %d'; | |
| 18 | + | rsBadDigitInValue = 'Mauvais chiffre %s dans la valeur, base = %d'; | |
| 19 | + | ||
| 20 | + | const | |
| 21 | + | Digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
| 22 | + | ||
| 23 | + | // Exprime la valeur dans la base voulue. | |
| 24 | + | function IntToBaseStr(Value, BaseOut: integer): String; | |
| 25 | + | var | |
| 26 | + | rmndr: integer; // reste | |
| 27 | + | begin | |
| 28 | + | // validité de la base ? | |
| 29 | + | if not InRange(BaseOut, 2, Length(Digits)) then | |
| 30 | + | raise ERangeError.CreateFmt(rsBadBase, [BaseOut, 2, length(Digits)]); | |
| 31 | + | // Décomposition de la valeur, la variable prend à chaque fois la valeur du diviseur | |
| 32 | + | // L'instruction repeat permet de tenir compte de la valeur 0 | |
| 33 | + | Result := ''; | |
| 34 | + | repeat | |
| 35 | + | divmod(Value, BaseOut, Value, rmndr); | |
| 36 | + | Result := Digits[rmndr+1] + Result; | |
| 37 | + | until Value = 0; | |
| 38 | + | End; | |
| 39 | + | ||
| 40 | + | // Transforme la chaîne d'un nombre exprimé dans la base paramètre en l'entier équivalent. | |
| 41 | + | // Si la chaîne est vide, le résultat est 0. | |
| 42 | + | // Attention : Pas de vérification de dépassement d'entier. il faut utiliser des valeurs raisonnables | |
| 43 | + | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; | |
| 44 | + | var | |
| 45 | + | d: Char; | |
| 46 | + | digit: Integer; | |
| 47 | + | begin | |
| 48 | + | Result := 0; | |
| 49 | + | for d in Value do | |
| 50 | + | begin | |
| 51 | + | digit := Pos(d, Digits) - 1; | |
| 52 | + | if not InRange(digit, 0, BaseIn-1) then | |
| 53 | + | raise ERangeError.CreateFmt(rsBadDigitInValue, [d, BaseIn]); | |
| 54 | + | Result := Result * BaseIn + digit; | |
| 55 | + | end; | |
| 56 | + | End; | |
| 57 | + | ||
| 58 | + | // Conversion directe de nombre exprimé dans une base vers une autre | |
| 59 | + | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; | |
| 60 | + | begin | |
| 61 | + | // le cas BaseIn=BaseOut n'est volontairement pas traité de manière spécifique | |
| 62 | + | // en vue des tests unitaires. | |
| 63 | + | Result := IntToBaseStr(BaseStrToInt(ValueIn, BaseIn), BaseOut); | |
| 64 | + | End; | |
| 65 | + | ||
| 66 | + | End. | |
| 67 | + | ``` | |
| 68 | + | ||
| 69 | + | --- | |
| 70 | + | *From the Free Pascal wiki, page "Base converting/fr", as it stood on 2020-02-09 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).* |
