给一个不一样的方法,用联合实现,我也常这样做,经常这样转换。简单省事type TrecBuf = packed record case Byte of 0: (b: array[0..3] of Byte); 1: (w: single); end;var x : single; testdata :TrecBuf; begin testdata.w := 2.17; testdata.b[0] := $48; testdata.b[1] := $E1; testdata.b[2] := $A; testdata.b[3] := $40; x := testdata.w; end;
type TrecBuf = packed record case Byte of 0: (b: array[0..3] of Byte); 1: (w: single); end; var x : single; testdata :TrecBuf; begin //testdata.w := 2.17; 我测试使用了,多出来一句。 testdata.b[0] := $48; testdata.b[1] := $E1; testdata.b[2] := $A; testdata.b[3] := $40; x := testdata.w; end;
LW, LW2: LongWord;
P: PByte;
begin
LW2 := 4269738304;
Showmessage(Format('%4x',[LW2])); CopyMemory(@LW, @LW2, SizeOf(LongWord));
showmessage(inttostr(LW));
Showmessage(Format('%4x',[LW])); LW := 0; {重新赋值为0}
P := @LW;
P^ := $40;
Inc(P);
P^ := $09;
Inc(P);
P^ := $7F;
Inc(P);
P^ := $FE;
showmessage(inttostr(LW));
Showmessage(Format('%4x',[LW]));
end;{如果LW并不是整型,可以声明一个整型指针指向LW所在4个字节的空间,也可以强制转换}
var
dwSrc : Integer;
snglDst : Single;
begin
dwSrc := $400AE148;
CopyMemory(@snglDst, @dwSrc, SizeOf(Single));
Showmessage(Format('%f',[snglDst])); dwSrc := $40097FFF;
CopyMemory(@snglDst, @dwSrc, SizeOf(Single));
Showmessage(Format('%f',[snglDst]));
end;
TrecBuf = packed record
case Byte of
0: (b: array[0..3] of Byte);
1: (w: single);
end;var
x : single;
testdata :TrecBuf;
begin testdata.w := 2.17; testdata.b[0] := $48;
testdata.b[1] := $E1;
testdata.b[2] := $A;
testdata.b[3] := $40; x := testdata.w;
end;
TrecBuf = packed record
case Byte of
0: (b: array[0..3] of Byte);
1: (w: single);
end; var
x : single;
testdata :TrecBuf;
begin //testdata.w := 2.17; 我测试使用了,多出来一句。 testdata.b[0] := $48;
testdata.b[1] := $E1;
testdata.b[2] := $A;
testdata.b[3] := $40; x := testdata.w;
end;