Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Object-Pascal / Delphi-Language (https://www.delphipraxis.net/35-library-object-pascal-delphi-language/)
-   -   Delphi StrToInt für "normale Zahlen" (nicht hexadezimale Zahlen) (https://www.delphipraxis.net/65263-strtoint-fuer-normale-zahlen-nicht-hexadezimale-zahlen.html)

sakura 14. Mär 2006 12:28


StrToInt für "normale Zahlen" (nicht hexadezimale
 
In dem Thread StrToInt löst keine exception aus waren einige verwundert, das StrToInt, StrToIntDef und Val auch Hexadezimale Zahlenstrings als solche erkennen. Da dieses Verhalten nicht dokumentiert ist, kann dieses zu unerwarteten Nebeneffekten führen. Die folgenden drei Funtkionen ändern dieses Verhalten von Delphi und sollten in diesen Fällen Abhilfe schaffen.

Delphi-Quellcode:
function ValNoHex(const s: String; var code: Integer): Longint;
var
  I: Integer;
  Negative: Boolean;
begin
  I := 1;
  code := -1;
  Result := 0;
  Negative := False;

  while (I <= Length(s)) and (s[I] = ' ') do
    Inc(I);

  if I > Length(s) then Exit;
  case s[I] of
    '-': begin
      Negative := True;
      Inc(I);
    end;
    '+': begin
      Inc(I);
    end;
  end;

  while I <= Length(s) do
  begin
    if (Result > (High(Result) div 10)) or (not (s[I] in ['0'..'9'])) then
    begin
      code := I;
      Exit;
    end;
    Result := Result * 10 + Ord(s[I]) - Ord('0');
    Inc(I);
  end;
  if Negative then
  begin
    Result := -Result;
  end;
  code := 0;
end;

function StrToIntNoHex(S: string): Longint;
var
  E: Integer;
begin
  Result := ValNoHex(S, E);
  if E <> 0 then raise EConvertError.CreateResFmt(@SInvalidInteger, [S]);
end;

function StrToIntDefNoHex(S: string; Default: LongInt): Longint;
var
  E: Integer;
begin
  Result := ValNoHex(S, E);
  if E <> 0 then
    Result := Default;
end;
...:cat:...

[edit=Chakotay1308]Klassifizierung. Mfg, Chakotay1308[/edit]


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:35 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