Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi HI und LO liefern falsche Werte (https://www.delphipraxis.net/183328-hi-und-lo-liefern-falsche-werte.html)

Codix32 1. Jan 2015 18:21

Delphi-Version: 2005

HI und LO liefern falsche Werte
 
Was mache ich da falsch?

Delphi-Quellcode:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
    int:Integer;
begin
  if Key = VK_Return then
    begin
     int:=strtoint(Edit1.text);
     Label4.Caption:= 'ganze Zahl: '+ inttostr(int);
     Label2.Caption:= 'HI: '+ inttostr(hi(int));
     Label3.Caption:= 'LO: '+ inttostr(lo(int));
end;
end;
Bei 2345 bekomme ich als 'HI' die Zahl 9 und bei 'LO' die Zahl 41 ?!?
Sollte da nicht HI 23 und LO 45 rauskommen?

mkinzler 1. Jan 2015 18:36

AW: HI und LO liefern falsche Werte
 
Nein, denn 9 * 256 = 2304 + 41 = 2345

Codix32 1. Jan 2015 18:57

AW: HI und LO liefern falsche Werte
 
Zitat:

Zitat von mkinzler (Beitrag 1285124)
Nein, denn 9 * 256 = 2304 + 41 = 2345

Da habe ich wohl was falsch verstanden:
Habe diesen Code gefunden, der da was ganz anderes ausgibt...

Delphi-Quellcode:
Example code : Illustrate Ho and Lo functions
 var
   i : Integer;

 begin
  i := $2345; // $2345 hex : $23 hi byte, $45 lo byte
   ShowMessage(Format('Integer = $%x', [i]));
   ShowMessage(Format('Hi byte = $%x', [Hi(i)]));
   ShowMessage(Format('Lo byte = $%x', [Lo(i)]));
 end;
Show full unit code

Integer = $2345
Hi byte = $23
Lo byte = $45


Hm, die Hi-LO Functionen funktionieren da wohl auch nur mit Word (0..65535) und nicht mit Integer(...-3-2-1 0 1 2 3...)?

Gut für meinen Zweck mache ich mir einen:
Delphi-Quellcode:
Type
  TMyshrtInt = Record
     h,l : shortint
  end;
Eigentlich könnte man da auch statt 'ShortInt' Integer verwenden.
Delphi 2005 und die CPU nutzt doch die vollen 32Bit. Und Shortint bringt wohl keinen Schnelligkeitsvorteil.

mkinzler 1. Jan 2015 19:03

AW: HI und LO liefern falsche Werte
 
Doch die sollten auch mit Integer funktionieren. Sie gehen aber Standardmässig von Dezimalzahlen aus und nicht von Zahlen in Hex.
Delphi-Quellcode:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
    int:Integer;
begin
  if Key = VK_Return then
    begin
     int:=strtoint('$'+Edit1.text);
     Label4.Caption:= 'ganze Zahl: '+ inttostr(int);
     Label2.Caption:= 'HI: '+ inttostr(hi(int));
     Label3.Caption:= 'LO: '+ inttostr(lo(int));
end;

Codix32 1. Jan 2015 19:15

AW: HI und LO liefern falsche Werte
 
Zitat:

Zitat von mkinzler (Beitrag 1285129)
Doch die sollten auch mit Integer funktionieren. Sie gehen aber Standardmässig von Dezimalzahlen aus und nicht von Zahlen in Hex.
Delphi-Quellcode:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
var
    int:Integer;
begin
  if Key = VK_Return then
    begin
     int:=strtoint('$'+Edit1.text);
     Label4.Caption:= 'ganze Zahl: '+ inttostr(int);
     Label2.Caption:= 'HI: '+ inttostr(hi(int));
     Label3.Caption:= 'LO: '+ inttostr(lo(int));
end;

Danke @mKinzler, ich glaube, jetzt ist der Groschen gefallen :oops:


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