delphi 里如何实现 VC++ 的 memcpy()函数的功能,如把 FE 7F 09 40 转换成 float 型2.17 ,API 怎么实现

解决方案 »

  1.   

    在Delphi里对应于C++的memcpy()的函数是CopyMemory但,对你说的 “把 FE 7F 09 40 转换成 float 型2.17 ”这话不懂。
      

  2.   

    就是 将内存中的十六进制的 FE 7F 09 40 转换成 拷贝出来 然后 转换成float 型的数据
      

  3.   

    把这串值copy到一个Pdouble指针处, 直接取值
      

  4.   

    才4个字节? 那就Copy给Real指针.你确定这串值, 就是IEEE754标准的2.17的浮点表示吗? 如果是某种自定义的浮点格式, 你必须自己手工转换.
      

  5.   

    {我无能力控制浮点数,下面是整型的一些演示}var
      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个字节的空间,也可以强制转换}
      

  6.   

    4字节的浮点数是single, 我搞错了.2.17的4字节浮点数按照IEE754表示应该是 400AE148, 你的那串数值是2.15, 代码如下 (注意字节序问题)
    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;
      

  7.   

    给一个不一样的方法,用联合实现,我也常这样做,经常这样转换。简单省事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;
      

  8.   

    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;