Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Algorithmen (https://www.delphipraxis.net/28-library-algorithmen/)
-   -   Delphi Octal zu Integer und umgekehrt (https://www.delphipraxis.net/6504-octal-zu-integer-und-umgekehrt.html)

Daniel B 13. Jul 2003 16:09


Octal zu Integer und umgekehrt
 
Hallo,

hiermit kann man ein IntegerWert und eine Octalzahl umwandeln.
Delphi-Quellcode:
function OctToInt(sValue: String): LongInt;
var
  iFor, iRes: Integer;
begin
  iRes := 0;
  for iFor := 1 to Length(sValue) do
  begin
//    iRes := iRes * 8 +StrToInt(Copy(sValue, iFor, 1));
    iRes := iRes * 8 +(Byte(sValue[iFor]) - Byte('0')); //Besserer Vorschlag von jbg
  end;
  Result := iRes;
end;
Und so Aufrufen:
Delphi-Quellcode:
procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToStr(OctToInt('77')));
end;
Grüsse, Daniel :hi:

Daniel B 13. Jul 2003 16:10

Re: Octal zu Integer und umgekehrt
 
Hallo,

hier ist auch schon das Gegenstück dazu:
Delphi-Quellcode:
function IntToOct(iValue: LongInt): String;
var
  iRest: LongInt;
  sOct: String;
  iFor: Integer;
begin
  sOct := '';
  while iValue > 0 do
  begin
   iRest := iValue mod 8;
   iValue := iValue div 8;
   sOct := IntToStr(iRest) + sOct;
  end;
  Result := sOct;
end;
So aufrufen:
Delphi-Quellcode:
procedure TForm1.Button3Click(Sender: TObject);
begin
  ShowMessage(IntToOct(100));
end;
Grüsse, Daniel :hi:

jbg 13. Jul 2003 16:36

Re: Octal zu Integer und umgekehrt
 
Zitat:

Zitat von Daniel B
StrToInt(Copy(sValue, iFor, 1));

Das kannst du doch sicherlich schöner und vor allem schneller.
Delphi-Quellcode:
StrToInt(sValue[iFor]));
Und noch schneller:
Delphi-Quellcode:
(Byte(sValue[iFor]) - Byte('0'));


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:50 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz