Einzelnen Beitrag anzeigen

tofse

Registriert seit: 6. Jun 2011
Ort: Saarlouis
131 Beiträge
 
Delphi XE Professional
 
#1

HexString to IEEE574 64 Bit

  Alt 14. Mai 2018, 12:13
Hallo,

ich habe mir das hier zusammengebaut. Meine Frage ist, geht das noch optimaler, mit evtl. Delphi eigenen Funktionen?

Delphi-Quellcode:
function ByteArrayToHexString(BA: TBytes; Sep: string = ''): string;
var
  i, k: integer;
begin
  result:='';

  if Sep='then begin
     for i:=low(BA) to high(BA) do
       result := result + IntToHex(BA[i], 2);
  end else begin
     k:=high(BA);
     for i:=low(BA) to k do begin
        result:= result + IntToHex(BA[i], 2);
        if k<>i then result := result + Sep;
     end;
  end;
end;

function HexToBin(Hexadecimal: string): string;
const
  BCD: array [0..15] of string =
    ('0000', '0001', '0010', '0011', '0100', '0101', '0110', '0111',
    '1000', '1001', '1010', '1011', '1100', '1101', '1110', '1111');
var
  i: integer;
begin
  for i := Length(Hexadecimal) downto 1 do
    Result := BCD[StrToInt('$' + Hexadecimal[i])] + Result;
end;

function BinToInt(Value: string): Integer;
var
  i, iValueSize: Integer;
begin
  Result := 0;
  iValueSize := Length(Value);
  for i := iValueSize downto 1 do
    if Value[i] = '1then Result := Result + (1 shl (iValueSize - i));
end;

Function VariantToBytes(Const Value: Variant): TBytes;
Var
  Size: Integer;
  pData: Pointer;
Begin
  Size := Succ(VarArrayHighBound(Value, 1) - VarArrayLowBound(Value, 1));
  SetLength(Result, Size);
  pData := VarArrayLock(Value);
  Try
    Move(pData^, Pointer(Result)^, Size);
  Finally
    VarArrayUnlock(Value);
  End;
End;

function OB2Result(const OrigDicom:OleVariant):Extended;
  var hexstr,binStr,mantisse,exponent:String;
      mantisseExt,exponentExt,Ergebnis:Extended;
      I:Integer;
begin
  hexstr:= ByteArrayToHexString(VariantToBytes(OrigDicom));
  // little Endian
  hexstr:=copy(hexstr,15,2)+copy(hexstr,13,2)+copy(hexstr,11,2)+copy(hexstr,9,2)+copy(hexstr,7,2)+copy(hexstr,5,2)+copy(hexstr,3,2)+copy(hexstr,1,2);
  binStr:=HexToBin(hexstr);
  mantisse:=copy(binstr,13,52);
  mantisseExt:=0.0;
  for I := 1 to length(mantisse) do
  begin
    if copy(mantisse,i,1)='1then
    begin
      mantisseExt:=power(2,-i)+mantisseExt;
    end;
  end;
  mantisseExt:=mantisseExt + 1.0;
  exponent:=copy(binstr,2,11);
  exponentExt:=BinToInt(exponent)-1023;
  Ergebnis := 0.0;
  if copy(binstr,1,1) = '0then
    Ergebnis:=power(2,exponentExt) * mantisseExt
  else
    Ergebnis:=-1 * power(2,exponentExt) * mantisseExt;
  Result:=Ergebnis;
end;

Als Beispiel z.B. E17A14AE47613740 entspricht 23.38

Die Übergabe hier
function OB2Result(const OrigDicom:OleVariant):Extended; bekomme ich als OleVariant, das zur Info.

Das alles funktioniert, bin echt froh, dass ich es hinbekommen habe, jetzt geht es ans Optimieren.
Vielleicht gibt es den einen oder anderen Tipp?

Gruß
Christof
  Mit Zitat antworten Zitat