Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   StrToInt löst keine exception aus (https://www.delphipraxis.net/65244-strtoint-loest-keine-exception-aus.html)

GoTo0815 14. Mär 2006 09:24


StrToInt löst keine exception aus
 
Delphi-Quellcode:
try
  i:= StrToInt(DBEdit20.Text);
except
  begin
    ShowMessage('Seriennummer Falsch! Bitte nochmals eingeben!');
    DBEdit20.SetFocus;
  end;
end;
Hallo, mein problem ist eigentlich ganz einfach zu beschreiben.

Wenn der String 'x00001' in das Editfeld eingetragen wird, dann löst das keine Exception aus, warum?

Gruß

Hendrik

freak4fun 14. Mär 2006 09:26

Re: StrToInt löst keine exception aus
 
Weil x keine Zahl ist. :)

MfG
freak

Angel4585 14. Mär 2006 09:29

Re: StrToInt löst keine exception aus
 
Habt iht mal seine Frage gelesen? Es löst KEINE Exception aus..
müsste es aber wenn ich StrToInt('x0001') ausführe

mquadrat 14. Mär 2006 09:30

Re: StrToInt löst keine exception aus
 
Also les ich falsch, oder die anderen? :D

Ich tippe, dass x00001 als Hexadezimalwert erkannt und entsprechend in das Dezimalsystem umgerechnet wird. Daher keine Exception.

ste_ett 14. Mär 2006 09:34

Re: StrToInt löst keine exception aus
 
Zitat:

Zitat von mquadrat
Ich tippe, dass x00001 als Hexadezimalwert erkannt und entsprechend in das Dezimalsystem umgerechnet wird. Daher keine Exception.

Und genauso ist es. :)

mquadrat 14. Mär 2006 09:38

Re: StrToInt löst keine exception aus
 
Und wenn ich mich noch mehr richtig erinner wird 00001 als Oktalzahl erkannt ;)

Daniel G 14. Mär 2006 09:41

Re: StrToInt löst keine exception aus
 
Aber durchaus ne interessante Sache. :zwinker:

Ohh...ne'n Kieler... :cheers:

GoTo0815 14. Mär 2006 10:43

Re: StrToInt löst keine exception aus
 
Aha, das ist ja schon mal interessant.

Aber wieso ist X ein Teil einer Hexadezimalzahl? Das geht doch nur bis 'F', oder.

Zudem ist sehr interessant, dass das Ergebnis von i = 1 ist.

Gruß

Hendrik aus Kiel :-)

Klaus01 14. Mär 2006 10:47

Re: StrToInt löst keine exception aus
 
x markiert das nun eine Hexazeizmalzahl kommt.
Hex(Basis16) 00001 ist auch nur 1.

x00011 sollte dann als Integer 17 geben

Grüße
Klaus

Christian Seehase 14. Mär 2006 12:04

Re: StrToInt löst keine exception aus
 
Moin Zusammen,

um die Aussage von Klaus noch etwas zu vervollständigen:
0x ist in C, was $ in Delphi ist, und der Delphi-Compiler erkennt das, dummerweise, auch als gültig an.

sakura 14. Mär 2006 12:24

Re: StrToInt löst keine exception aus
 
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 :)

...:cat:...

Hawkeye219 14. Mär 2006 13:42

Re: StrToInt löst keine exception aus
 
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


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