Delphi双字节数组

Art*_*tik 12 delphi pointers

将double值转换为其字节表示的简单方法是什么?我尝试使用如下指针:

var
  double_v:double;
  address:^double;
....
double_v:=100;
address:=@double_v;
Run Code Online (Sandbox Code Playgroud)

但我的每一个概念:如何从地址读取8个字节,以"AV"结束.

alz*_*mar 16

使用变体记录

Type
  TDoubleAndBytes = Record
    case boolean of
      false : (dabDouble : Double);
      true  : (dabBytes : Array [0..7] Of Byte);
  end;
Run Code Online (Sandbox Code Playgroud)

将double值分配给dabDouble并通过dabBytes读取字节

var
  myVar : TDoubleAndBytes;
  i : integer;

begin
  myVar.dabDouble := 100;
  for i:=0 to 7 do write(myVar.dabBytes[i]);
Run Code Online (Sandbox Code Playgroud)


LU *_* RD 10

在XE3中,有简单类型的记录助手TDoubleHelper.

这有效:

var
  d : Double;
  i : Integer;
begin
  d := 100.0;
  for i := 0 to 7 do
    WriteLn(d.Bytes[i]);
end;
Run Code Online (Sandbox Code Playgroud)

在XE2中有一个声明TDoubleRec,它是一个高级记录.

例:

var
  dRec : TDoubleRec;
  i : Integer;
begin
  dRec := 100.0;
  for i := 0 to 7 do
    WriteLn(dRec.Bytes[i]);
end;
Run Code Online (Sandbox Code Playgroud)

访问double的字节的另一个常见选项是使用typecast:

type
  TDoubleAsByteArr = array[0..7] of byte;
var
  d : Double;
  i : Integer;
begin
  d := 100.0;
  for i := 0 to 7 do
    WriteLn(TDoubleAsByteArr(d)[i]);
end;
Run Code Online (Sandbox Code Playgroud)


bum*_*mmi 6

两个使用"绝对"的例子......

用作功能

function GetDoubleByte(MyDouble: Double; Index: Byte): Byte;
var
  Bytes: array[0..7] of Byte absolute MyDouble;
begin
  Result := Bytes[Index];
end;



procedure TForm1.Button1Click(Sender: TObject);
var
 MyDouble:Double;
 DoubleBytes:Array[0..7] of Byte absolute MyDouble; // direct local use
begin
  MyDouble := 17.123;
  ShowMessage(IntToStr(DoubleBytes[0])); // local usage

  ShowMessage(IntToStr(GetDoubleByte(MyDouble,0))); // via function call

end;
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.所有你的建议让我看到了德尔福更新的(在我的生活中). (2认同)