Difference between revisions of "Base converting"
From Lazarus wiki
Jump to navigationJump to search (Created page with " {{Base_converting}} This unit will allow you to switch from one number base to another, each base is chosen ini the range 2..36. <syntaxhighlight> unit BaseConvert; inter...") |
(removed trailing white space; added hint to binStr, octStr, str, hexStr in system uni) |
||
Line 2: | Line 2: | ||
{{Base_converting}} | {{Base_converting}} | ||
− | This unit will allow you to switch from one number base to another, each base is chosen | + | This unit will allow you to switch from one number base to another, each base is chosen in the range <code>2..36</code>. |
<syntaxhighlight> | <syntaxhighlight> | ||
− | |||
unit BaseConvert; | unit BaseConvert; | ||
Line 11: | Line 10: | ||
function IntToBaseStr(Value, BaseOut: integer): String; | function IntToBaseStr(Value, BaseOut: integer): String; | ||
− | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; | + | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; |
− | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; | + | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; |
implementation | implementation | ||
− | resourceString | + | resourceString |
rsBadBase = 'Base %d invalide, elle doit être comprise entre' | rsBadBase = 'Base %d invalide, elle doit être comprise entre' | ||
− | +' %d et %d'; | + | +' %d et %d'; |
rsBadDigitInValue = 'Mauvais chiffre %s dans la valeur, base = %d'; | rsBadDigitInValue = 'Mauvais chiffre %s dans la valeur, base = %d'; | ||
Line 24: | Line 23: | ||
Digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | Digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | ||
− | // Exprime la valeur dans la base voulue. | + | // Exprime la valeur dans la base voulue. |
function IntToBaseStr(Value, BaseOut: integer): String; | function IntToBaseStr(Value, BaseOut: integer): String; | ||
− | var | + | var |
− | rmndr: integer; // reste | + | rmndr: integer; // reste |
− | begin | + | begin |
− | // validité de la base ? | + | // validité de la base ? |
− | if not InRange(BaseOut, 2, Length(Digits)) then | + | if not InRange(BaseOut, 2, Length(Digits)) then |
− | raise ERangeError.CreateFmt(rsBadBase, [BaseOut, 2, length(Digits)]); | + | raise ERangeError.CreateFmt(rsBadBase, [BaseOut, 2, length(Digits)]); |
− | // Décomposition de la valeur, la variable prend à chaque fois la valeur du diviseur | + | // Décomposition de la valeur, la variable prend à chaque fois la valeur du diviseur |
− | // L'instruction repeat permet de tenir compte de la valeur 0 | + | // L'instruction repeat permet de tenir compte de la valeur 0 |
− | Result := ''; | + | Result := ''; |
− | repeat | + | repeat |
− | divmod(Value, BaseOut, Value, rmndr); | + | divmod(Value, BaseOut, Value, rmndr); |
− | Result := Digits[rmndr+1] + Result; | + | Result := Digits[rmndr+1] + Result; |
− | until Value = 0; | + | until Value = 0; |
End; | End; | ||
− | // Transforme la chaîne d'un nombre exprimé dans la base paramètre en l'entier équivalent. | + | // Transforme la chaîne d'un nombre exprimé dans la base paramètre en l'entier équivalent. |
− | // Si la chaîne est vide, le résultat est 0. | + | // Si la chaîne est vide, le résultat est 0. |
− | // Attention : Pas de vérification de dépassement d'entier. il faut utiliser des valeurs raisonnables | + | // Attention : Pas de vérification de dépassement d'entier. il faut utiliser des valeurs raisonnables |
− | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; | + | function BaseStrToInt(Value: String; BaseIn: Integer): Integer; |
− | var | + | var |
− | d: Char; | + | d: Char; |
− | digit: Integer; | + | digit: Integer; |
− | begin | + | begin |
− | Result := 0; | + | Result := 0; |
− | for d in Value do | + | for d in Value do |
− | begin | + | begin |
− | digit := Pos(d, Digits) - 1; | + | digit := Pos(d, Digits) - 1; |
− | if not InRange(digit, 0, BaseIn-1) then | + | if not InRange(digit, 0, BaseIn-1) then |
− | raise ERangeError.CreateFmt(rsBadDigitInValue, [d, BaseIn]); | + | raise ERangeError.CreateFmt(rsBadDigitInValue, [d, BaseIn]); |
− | Result := Result * BaseIn + digit; | + | Result := Result * BaseIn + digit; |
− | end; | + | end; |
End; | End; | ||
− | // Conversion directe de nombre exprimé dans une base vers une autre | + | // Conversion directe de nombre exprimé dans une base vers une autre |
− | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; | + | function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String; |
− | begin | + | begin |
− | // le cas BaseIn=BaseOut n'est volontairement pas traité de manière spécifique | + | // le cas BaseIn=BaseOut n'est volontairement pas traité de manière spécifique |
− | // en vue des tests unitaires. | + | // en vue des tests unitaires. |
− | Result := IntToBaseStr(BaseStrToInt(ValueIn, BaseIn), BaseOut); | + | Result := IntToBaseStr(BaseStrToInt(ValueIn, BaseIn), BaseOut); |
End; | End; | ||
End. | End. | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | < | + | |
+ | For your information: For bases two, eight, ten, and sixteen, you can use <code>binStr</code>, <code>OctStr</code>, <code>str</code> and <code>hexStr</code> from the automatically included <code>system</code> unit. |
Revision as of 17:58, 21 January 2018
│
English (en) │
français (fr) │
This unit will allow you to switch from one number base to another, each base is chosen in the range 2..36
.
unit BaseConvert;
interface
function IntToBaseStr(Value, BaseOut: integer): String;
function BaseStrToInt(Value: String; BaseIn: Integer): Integer;
function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String;
implementation
resourceString
rsBadBase = 'Base %d invalide, elle doit être comprise entre'
+' %d et %d';
rsBadDigitInValue = 'Mauvais chiffre %s dans la valeur, base = %d';
const
Digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
// Exprime la valeur dans la base voulue.
function IntToBaseStr(Value, BaseOut: integer): String;
var
rmndr: integer; // reste
begin
// validité de la base ?
if not InRange(BaseOut, 2, Length(Digits)) then
raise ERangeError.CreateFmt(rsBadBase, [BaseOut, 2, length(Digits)]);
// Décomposition de la valeur, la variable prend à chaque fois la valeur du diviseur
// L'instruction repeat permet de tenir compte de la valeur 0
Result := '';
repeat
divmod(Value, BaseOut, Value, rmndr);
Result := Digits[rmndr+1] + Result;
until Value = 0;
End;
// Transforme la chaîne d'un nombre exprimé dans la base paramètre en l'entier équivalent.
// Si la chaîne est vide, le résultat est 0.
// Attention : Pas de vérification de dépassement d'entier. il faut utiliser des valeurs raisonnables
function BaseStrToInt(Value: String; BaseIn: Integer): Integer;
var
d: Char;
digit: Integer;
begin
Result := 0;
for d in Value do
begin
digit := Pos(d, Digits) - 1;
if not InRange(digit, 0, BaseIn-1) then
raise ERangeError.CreateFmt(rsBadDigitInValue, [d, BaseIn]);
Result := Result * BaseIn + digit;
end;
End;
// Conversion directe de nombre exprimé dans une base vers une autre
function BaseToBase(ValueIn: String; baseIn, BaseOut: Integer): String;
begin
// le cas BaseIn=BaseOut n'est volontairement pas traité de manière spécifique
// en vue des tests unitaires.
Result := IntToBaseStr(BaseStrToInt(ValueIn, BaseIn), BaseOut);
End;
End.
For your information: For bases two, eight, ten, and sixteen, you can use binStr
, OctStr
, str
and hexStr
from the automatically included system
unit.