AGB  ·  Datenschutz  ·  Impressum  







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

Real-Bytes vertauschen

Ein Thema von Pelzini · begonnen am 20. Sep 2005 · letzter Beitrag vom 20. Sep 2005
Antwort Antwort
Pelzini

Registriert seit: 20. Sep 2005
4 Beiträge
 
Delphi 6 Professional
 
#1

Real-Bytes vertauschen

  Alt 20. Sep 2005, 10:49
Hallo,

ich habe ein für Profis wahrscheinlich ziemlich einfach zu lösendes Problem:

ich muss eine Real-Datei einlesen, die aus einer anderen Computer-Welt kommt (LabVIEW) und in der die 8 Byte der Real-Werte umgedreht sind (low => high). Wie kann man die wieder so drehen (high => low), dass Delphi 6 daraus den richtigen Real-Wert erkennt ?

Schon mal danke im Voraus

Pelzini
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: Real-Bytes vertauschen

  Alt 20. Sep 2005, 11:13
Hallo Pelzini,

herzlich willkommen in der Delphi-PRAXiS.

So etwa könnte es funktionieren:

Delphi-Quellcode:
function ConvertDouble(d: double): double;
var
  pbIn, pbOut: PByte;
begin
  pbIn := @d;
  Inc(pbIn, SizeOf(double));
  pbOut := @Result;
  repeat
    Dec(pbIn);
    pbOut^:= pbIn^;
    Inc(pbOut);
  until pbIn = @d;
end;
Und so natürlich auch:

Delphi-Quellcode:
procedure SwapBytes(var buffer; size: word);
var
  b: byte;
  pbIn, pbOut: PByte;
begin
  pbIn := @buffer;
  Inc(pbIn, size - 1);
  pbOut := @buffer;
  while Cardinal(pbIn) > Cardinal(pbOut) do begin
    b := pbOut^;
    pbOut^:= pbIn^;
    pbIn^ := b;
    Dec(pbIn);
    Inc(pbOut);
  end;
end;
Grüße vom marabu
  Mit Zitat antworten Zitat
Benutzerbild von BlackJack
BlackJack

Registriert seit: 2. Jul 2005
Ort: Coesfeld
246 Beiträge
 
Delphi 2005 Personal
 
#3

Re: Real-Bytes vertauschen

  Alt 20. Sep 2005, 13:12
so ginge es auch, wenn die high/low-DWords des Doubles in H und L stehen und die umgedrehten bytes nachher in den DWords H2, L2 stehen sollen:
Delphi-Quellcode:
asm
  mov eax, H
  bswap eax

  mov edx, L
  bswap edx

  mov H2, edx
  mov L2, eax
end;
und um die beiden einzelnen DWords des Doubles in die Variablen H und L zu bekommen, kannst du sowas hier nehmen:
Delphi-Quellcode:
var H, L: Integer;
    D: Double;
    PD: ^Integer;
begin
D := 1234;
PD := @D;
H := Integer(PD^);
Inc(PD);
L := Integer(PD^);
end;
p.s.: ungetestet!

edit: bswap vertauscht einfach die bytes des Arguments, also aus z.b. "$ABCD" wird "$DCBA"
See my shadow changing, stretching up and over me.
Soften this old armor. Hoping I can clear the way
By stepping through my shadow, coming out the other side.
Step into the shadow. Forty six and two are just ahead of me.
  Mit Zitat antworten Zitat
Pelzini

Registriert seit: 20. Sep 2005
4 Beiträge
 
Delphi 6 Professional
 
#4

Re: Real-Bytes vertauschen

  Alt 20. Sep 2005, 14:43
Hallo marabu, hallo Black Jack,

danke, dass ihr so schnell reagiert habt. Marabu, ich habe deine erste Lösung (ConvertDouble) verwendet, und es klappt ganz einwandfrei. Ich hab's sogar verstanden, nachdem ich meine Kenntnisse über Pointer wieder etwas aufgefrischt habe. Die zweite Lösung und deine, Black Jack, werde ich interessehalber auch mal probieren. Nochmals vielen Dank!

Pelzini
  Mit Zitat antworten Zitat
Antwort Antwort


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 14:58 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