主题:如何将十六进制转成浮点数?
peterp
[专家分:0] 发布于 2006-08-04 17:53:00
小弟请教了,不胜感激.最好有编程.
回复列表 (共2个回复)
沙发
peterp [专家分:0] 发布于 2006-08-07 09:26:00
没高手吗?
板凳
nepenthe [专家分:460] 发布于 2006-08-07 18:05:00
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,IdGlobal,StrUtils,Math;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Button2: TButton;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
function BinToInt(Value: string): Integer;
var
i, iValueSize: Integer;
begin
Result := 0;
iValueSize := Length(Value);
for i := iValueSize downto 1 do
if Value[i] = '1' then Result := Result + (1 shl (iValueSize - i));
end;
var
Text : String;
Len:Integer;
Bin,E,M,i : integer;
str,s: string;
Ma,Ex,Dec : real;
begin
bin:=0;
Text:='46EB0A03';
s:='';
for i:=4 downto 1 do
s:=s+MidStr(Text,i*2-1,2);
Text:=s;
Len:=lENGTH(Text) DIV 2;
HexToBin(pchar(Text),@Bin,Len);
str:=IntToBin(bin);
i:=1;
if LeftStr(str,1)='1' then i:=-1;
s:=MidStr(str,2,8);
E:=BinToInt(s);
s:=MidStr(str,10,23);
M:=BinToInt(s);
E:=E-127;
Ex:=Power(2,E);
Ma:=1.0+(M/8388608);
Dec:=i*Ex*Ma;
Edit1.Text:=FloatToStr(Dec);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Dec : real;
i,E: integer;
str,S: string;
H : integer;
begin
Dec:=20.59375;
S:='1';
if Dec=Abs(dec) then S:='0';
i:=trunc(dec);
Str:=IntToBin(i);
E:=(Length(str)-pos('1',str))+127;
str:=RightStr(str,Length(str)-pos('1',str));
S:=S+ RightStr(IntToBin(E),8);
S:=S+str;
Dec:=Dec-i;
str:='';
for i:=1 to 32-Length(s) do
begin
Dec:=Dec*2;
if Dec>=1 then begin
str:=str+'1';
Dec:=Dec-1;
end
else str:=str+'0';
end;
s:=s+str;
h:=0;
for i:=1 to 32 do
if MidStr(s,i,1)='1' then
h:=h+trunc(power(2,(32-i)))
else h:=h+0;
Edit1.text:=IntToHex(h,2);
Edit2.Text:=s;
end;
end.
我来回复