Einzelnen Beitrag anzeigen

dGeek
(Gast)

n/a Beiträge
 
#10

AW: Delphi 7, Int64 und Ini.ReadInteger

  Alt 10. Sep 2016, 12:01
Das Thema ist alt, ja. Wenn jemand das ohne Hex-String machen möchte, ich hab es so gemacht:

Delphi-Quellcode:
function IsNumeric(const aString: string; const bAcceptNegativeNumbers: Boolean = True): Boolean;
var
 bRes: Boolean;
begin
 bRes := StrToInt64Def(aString, 0) = StrToInt64Def(aString, 1);

 if bRes and (not bAcceptNegativeNumbers) and (StrToInt64(aString) < 0) then
  bRes := False;

 Result := bRes;
end;

procedure IniWriteInt64(var IniFile: TIniFile; const Section: string; const Ident: string; Value: Int64);
begin
 IniFile.WriteString(Section, Ident, IntToStr(Value));
end;

function IniReadInt64(var IniFile: TIniFile; const Section: string; const Ident: string; Default: Int64): Int64;
var
 sTmp: String;
begin
 sTmp := IniFile.ReadString(Section, Ident, IntToStr(Default));

 if functions.IsNumeric(sTmp, False) then
  Result := StrToInt64(sTmp)
 else
  Result := Default;
end;
Erklärung zu IsNumeric:
wenn man einen Int64-Wert normal mit IniX.WriteInteger() speichert und der Wert größer als Integer (also 2147483647) ist, dann wird ein negativer Wert gespeichert.
IsNumeric prüft, ob der gelesene Wert ein negativer Wert ist. Wenn ja Default zurückgeben.

Das ist meine Lösung ohne Hex

Aufruf z.B.:
Delphi-Quellcode:
procedure TJabutMainForm.Button1Click(Sender: TObject);
var
 myIni: TIniFile;
 myInt64: Int64;
begin
 myIni := TIniFile.Create('myIni.ini');

 try
  // Speichern
  myInt64 := 91147483647;
  IniWriteInt64(myIni, 'Section', 'Ident', myInt64);

  // Lesen
  myInt64 := IniReadInt64(myIni, 'Section', 'Ident', 0);
 finally
  myIni.Free;
 end;
end;

Geändert von dGeek (10. Sep 2016 um 12:05 Uhr)
  Mit Zitat antworten Zitat