AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

StrToInt löst keine exception aus

Ein Thema von GoTo0815 · begonnen am 14. Mär 2006 · letzter Beitrag vom 14. Mär 2006
Antwort Antwort
Seite 2 von 2     12   
Benutzerbild von sakura
sakura

Registriert seit: 10. Jun 2002
Ort: München
11.412 Beiträge
 
Delphi 11 Alexandria
 
#11

Re: StrToInt löst keine exception aus

  Alt 14. Mär 2006, 12:24
Hier mal die nötigen Codes, damit keine Hex-Werte als Ergebnis zugelassen werden
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;
Anstatt StrToInt und StrToIntDef nutze StrToIntNoHex und StrToIntDefNoHex

......
Daniel W.
Ich bin nicht zurück, ich tue nur so
  Mit Zitat antworten Zitat
Hawkeye219

Registriert seit: 18. Feb 2006
Ort: Stolberg
2.227 Beiträge
 
Delphi 2010 Professional
 
#12

Re: StrToInt löst keine exception aus

  Alt 14. Mär 2006, 13:42
Kleine Ergänzung:

Einige Zahlen außerhalb des Long-Bereichs werden nicht zurückgewiesen sondern falsch konvertiert. Die Abfrage

Code:
if (Result > (High(Result) div 10)) or (not (s[I] in ['0'..'9'])) then
greift erst ab dem Wert 2147483650, der Long-Bereich ist aber [-2147483648..2147483647].
Borland hat dies in der Systemroutine _ValLong (im Assembler-Teil!) durch eine Abfrage des Overflow-Flags kompensiert.

Der Fehler tritt aber lediglich an den Intervallrändern auf und dürfte nur für die Anwendungsfälle interessant sein, in denen der Long-Bereich exakt abgedeckt werden soll.

Gruß Hawkeye
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 2     12   


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 23:36 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