zoukankan      html  css  js  c++  java
  • 将四个BYTE数值转换成IEEE754标准的浮点数

    在工作中,经常使用到IEEE754格式的数据。IEEE754格式的数据占四个字节,好像Motorola格式和Intel格式的还不一样。

    由于工作中很少和他打交道(使用的软件内部已经处理),就没太在意。

    今天在编程时发现需要把四个BYTE类型的数据转换成IEEE754标准的数据,就编了一个函数处理一下。

     

     1 unit Unit2;
     2 
     3 interface
     4 
     5 uses
     6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     7   Dialogs, StdCtrls;
     8 
     9 type
    10   TForm2 = class(TForm)
    11     edt1: TEdit;
    12     edt2: TEdit;
    13     edt3: TEdit;
    14     edt4: TEdit;
    15     edt5: TEdit;
    16     btn1: TButton;
    17     procedure btn1Click(Sender: TObject);
    18   private
    19     { Private declarations }
    20   public
    21     { Public declarations }
    22   end;
    23 
    24 var
    25   Form2: TForm2;
    26 
    27 implementation
    28 
    29 {$R *.dfm}
    30 {将四个BYTE转换成IEEE754格式的数据}
    31 function PackByteToFloat(byte1,byte2,byte3,byte4:byte):Single;
    32 var
    33   input:array[1..4of byte; {定义一个数组存放输入的四个BYTE}
    34   output:PSingle;
    35 begin
    36   input[1] := byte1;
    37   input[2] := byte2;
    38   input[3] := byte3;
    39   input[4] := byte4;
    40   output := Addr(input);  {使用取地址的方法进行处理}
    41   Result := output^;      {得到了intel格式的数据}
    42 end;
    43 
    44 procedure TForm2.btn1Click(Sender: TObject);
    45 var
    46   byte1,byte2,byte3,byte4:byte;
    47 
    48 begin
    49   byte1 := StrToInt(edt1.Text);
    50   byte2 := StrToInt(edt2.Text);
    51   byte3 := StrToInt(edt3.Text);
    52   byte4 := StrToInt(edt4.Text);
    53 
    54   edt5.Text := FloatToStr(PackByteToFloat(byte1,byte2,byte3,byte4));
    55 
    56 end;
    57 
    58 end.
    59 

     

    {☆推荐☆:返利网,你网上购物,我返给您现金,实惠!省钱新门道}

  • 相关阅读:
    焦点Banner效果滚动
    缓冲效果
    招商银行购物网站的产品展示效果,循环播放
    神是怎么看待计算机的呢
    Liaoning Province保三成功
    JAR文件(文件格式)
    tamarin系列之5] 植入本地方法实现
    无线上把锁:WEP、WPA无线加密方式对比
    Tamarin
    V8 Javascript 引擎设计理念
  • 原文地址:https://www.cnblogs.com/dabiao/p/1669842.html
Copyright © 2011-2022 走看看