- Projet in the zip
-
- Unité UlpBase : unité pour la convertion
- -----------------
-
- unit UlpBase;
-
- interface
-
- uses Sysutils,
- UlpString;
-
- const MinBase = 2;
- const MaxBase = 36;
-
- Const Binaire = 2;
- Const Octal = 8;
- Const Decimal = 10;
- Const HexaDecimal = 16;
-
- type
- ClpBase = Class
- private
- function GetPoidsFort(lwNbr:LongWord; bBase:Byte):Byte;
- function GetMultiple(bBase, bExpo:Byte):LongWord;
- function GetChiffre(chrLettre:Char):Byte;
- function SetChiffre(bLettre:Byte):string;
- public
- // Other --> 10
- function BaseDix(strNbr:string; bBase:Byte):string;
- // 10 --> Other
- function BaseOther(lwNbr:LongWord; bBase:Byte):string;
- // Other --> Other
- function BasesConvert(strNbr:string; bBaseOld, bBaseNew:Byte):string;
- end;
-
- implementation
-
- /////////////////////////////////////
- // PRIVATE //
- /////////////////////////////////////
-
- function ClpBase.GetPoidsFort(lwNbr:LongWord; bBase:Byte):Byte;
- var bPF:Byte;
- begin
- bPF := 1;
- While( lwNbr > bBase) do
- begin
- lwNbr := (lwNbr div bBase);
- Inc(bPF);
- end;
- Result := bPF;
- end;
-
- function ClpBase.GetMultiple(bBase, bExpo:Byte):LongWord;
- var lwRes:LongWord;
- begin
- lwRes := 1;
- While(bExpo > 0)do
- begin
- lwRes := (lwRes * bBase);
- Dec(bExpo);
- end;
- Result := lwRes;
- end;
-
- function ClpBase.GetChiffre(chrLettre:Char):Byte;
- var bRes:Byte;
- begin
- case ( Ord(chrLettre) - 48) of
- 0..9:bRes := Ord(chrLettre) - 48;
- else
- bRes := Ord(chrLettre) + 10 - 65;
- end;
- Result := bRes;
- end;
-
- function ClpBase.SetChiffre(bLettre:Byte):string;
- var strRes:string;
- begin
- case bLettre of
- 0..9:strRes := IntToStr(bLettre);
- else
- strRes := Chr(65 + bLettre - 10);
- end;
- Result := strRes;
- end;
-
- /////////////////////////////////////
- // PUBLIC //
- /////////////////////////////////////
-
- function ClpBase.BaseDix(strNbr:string; bBase:Byte):string;
- var bPF, bPos:Byte;
- strRes:string;
- begin
- strRes := '0';
- bPF := UlpString.LenStr(StrNbr);
- bPos := 0;
- While(bPF > bPos)do
- begin
- Inc(bPos);
- strRes := IntToStr(StrToInt(strRes) + ( GetChiffre(strNbr[bPF - bPos + 1]) * GetMultiple(bBase, (bPos - 1)) ));
- end;
- Result := strRes;
- end;
-
- function ClpBase.BaseOther(lwNbr:LongWord; bBase:Byte):string;
- var bPF, bIndex:Byte;
- strRes:string;
- Passe:Boolean;
- begin
- strRes := '';
- Passe := False;
- bPF := GetPoidsFort(lwNbr, bBase);
- while (bPF > 0) do
- begin
- Passe := True;
- bIndex := (lwNbr div GetMultiple(bBase, bPF));
- lwNbr := lwNbr - ( bIndex * GetMultiple(bBase, bPF));
- strRes := strRes + SetChiffre(bIndex);
- Dec(bPF);
- end;
- if Passe then
- strRes := strRes + SetChiffre(lwNbr);
- if strRes[1] = '0' then
- strRes := UlpString.DecalLeft(strRes, 1);
- Result := strRes;
- end;
-
- function ClpBase.BasesConvert(strNbr:string; bBaseOld, bBaseNew:Byte):string;
- var strRes:string;
- lwRes:LongWord;
- begin
- strRes := BaseDix(strNbr, bBaseOld);
- lwRes := StrToInt(strRes);
- strRes := BaseOther(lwRes, bBaseNew);
- Result := strRes;
- end;
-
- end.
Projet in the zip
Unité UlpBase : unité pour la convertion
-----------------
unit UlpBase;
interface
uses Sysutils,
UlpString;
const MinBase = 2;
const MaxBase = 36;
Const Binaire = 2;
Const Octal = 8;
Const Decimal = 10;
Const HexaDecimal = 16;
type
ClpBase = Class
private
function GetPoidsFort(lwNbr:LongWord; bBase:Byte):Byte;
function GetMultiple(bBase, bExpo:Byte):LongWord;
function GetChiffre(chrLettre:Char):Byte;
function SetChiffre(bLettre:Byte):string;
public
// Other --> 10
function BaseDix(strNbr:string; bBase:Byte):string;
// 10 --> Other
function BaseOther(lwNbr:LongWord; bBase:Byte):string;
// Other --> Other
function BasesConvert(strNbr:string; bBaseOld, bBaseNew:Byte):string;
end;
implementation
/////////////////////////////////////
// PRIVATE //
/////////////////////////////////////
function ClpBase.GetPoidsFort(lwNbr:LongWord; bBase:Byte):Byte;
var bPF:Byte;
begin
bPF := 1;
While( lwNbr > bBase) do
begin
lwNbr := (lwNbr div bBase);
Inc(bPF);
end;
Result := bPF;
end;
function ClpBase.GetMultiple(bBase, bExpo:Byte):LongWord;
var lwRes:LongWord;
begin
lwRes := 1;
While(bExpo > 0)do
begin
lwRes := (lwRes * bBase);
Dec(bExpo);
end;
Result := lwRes;
end;
function ClpBase.GetChiffre(chrLettre:Char):Byte;
var bRes:Byte;
begin
case ( Ord(chrLettre) - 48) of
0..9:bRes := Ord(chrLettre) - 48;
else
bRes := Ord(chrLettre) + 10 - 65;
end;
Result := bRes;
end;
function ClpBase.SetChiffre(bLettre:Byte):string;
var strRes:string;
begin
case bLettre of
0..9:strRes := IntToStr(bLettre);
else
strRes := Chr(65 + bLettre - 10);
end;
Result := strRes;
end;
/////////////////////////////////////
// PUBLIC //
/////////////////////////////////////
function ClpBase.BaseDix(strNbr:string; bBase:Byte):string;
var bPF, bPos:Byte;
strRes:string;
begin
strRes := '0';
bPF := UlpString.LenStr(StrNbr);
bPos := 0;
While(bPF > bPos)do
begin
Inc(bPos);
strRes := IntToStr(StrToInt(strRes) + ( GetChiffre(strNbr[bPF - bPos + 1]) * GetMultiple(bBase, (bPos - 1)) ));
end;
Result := strRes;
end;
function ClpBase.BaseOther(lwNbr:LongWord; bBase:Byte):string;
var bPF, bIndex:Byte;
strRes:string;
Passe:Boolean;
begin
strRes := '';
Passe := False;
bPF := GetPoidsFort(lwNbr, bBase);
while (bPF > 0) do
begin
Passe := True;
bIndex := (lwNbr div GetMultiple(bBase, bPF));
lwNbr := lwNbr - ( bIndex * GetMultiple(bBase, bPF));
strRes := strRes + SetChiffre(bIndex);
Dec(bPF);
end;
if Passe then
strRes := strRes + SetChiffre(lwNbr);
if strRes[1] = '0' then
strRes := UlpString.DecalLeft(strRes, 1);
Result := strRes;
end;
function ClpBase.BasesConvert(strNbr:string; bBaseOld, bBaseNew:Byte):string;
var strRes:string;
lwRes:LongWord;
begin
strRes := BaseDix(strNbr, bBaseOld);
lwRes := StrToInt(strRes);
strRes := BaseOther(lwRes, bBaseNew);
Result := strRes;
end;
end.