阅读 143

delphi中10进制和16进制互转(支持浮点类型)

----------开发环境D7----------

有位数限制,超过位数,不会转换

注释乱码问题,以前的操作系统用繁体,后来改成简体的了,有乱码;

代码仅提供思路,不喜勿喷

-----------------------------------

 

 

--------------Unit----

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,Math ,StrUtils;

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Edit2: TEdit;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Edit1Change(Sender: TObject);
procedure Edit2Change(Sender: TObject);
private
function DecimalToHex(MYDecimal:Extended ):string;
function HexToDecimal(MYHex:String ):string ;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
const
Myarray:array[0..15] of string=(‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘
,‘9‘,‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘);
Myarray_X:array[1..8] of integer=(1,16,256,4096,65536,1048576,16777216,268435456);

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.DecimalToHex(MYDecimal: Extended): string;
var
i,Int,Int_12,Int_13,TempInt,SmallNum_count:Integer; //俱计 //RepetitionNUM碻吏计Repetitions瞷碻吏SmallNum_Icount计计
SmallNum:Extended; //计
Quotient:Integer; //坝计
Remainder:Integer; //緇计
RemainderStr,RemainderStr2:string;


begin
Edit2.Clear;
Result:=‘‘;
if Pos(‘.‘,FloatToStr(MYDecimal))>0 then //Τ计
begin
SmallNum:=Frac(MYDecimal);//计场だ
SmallNum_count:=0;
Int_12:=0;
Int_13:=0;
//if strtoint (Copy(FloatToStr(MYDecimal),Pos(‘.‘,FloatToStr(MYDecimal))+1,100))>0 then //计场だ
begin
while (SmallNum>0) and (SmallNum_count<14) do //讽计单0 ┪ 计计竒13 碞癶碻吏 (玂靡程12计)
begin
Inc(SmallNum_count);
SmallNum:=SmallNum*16;//

Int:= trunc(SmallNum) ; //俱计场だ
SmallNum:=SmallNum - trunc(SmallNum);//计场だ
if SmallNum_count=12 then
begin
if SmallNum>0 then
Int_12:=Int;
end
else if SmallNum_count=13 then
begin
Int_13:=Int;
Break ;//癶碻吏
end;
if SmallNum_count<12 then //12ぇ玡常ノ硂
RemainderStr2:=RemainderStr2+Myarray[Int];
end;
if Int_13>4 then //材4,12碞秈讽12琌F碞ぃ秈
begin
if Int_12<>15 then
inc(Int_12);
end ;

if Int_12>0 then
RemainderStr2:=RemainderStr2+Myarray[Int_12];

end;
Int:=trunc(MYDecimal) ;
//Int:=strtoint (Copy(FloatToStr(MYDecimal),1,Pos(‘.‘,FloatToStr(MYDecimal))-1));
if Int>0 then //俱计场だ
begin

Quotient:= Int;
while Quotient>0 do
begin
Quotient:=( Int div 16 ); //
Remainder:= ( Int mod 16 );//
Int:=Quotient;
RemainderStr:=Myarray[Remainder]+ RemainderStr;
end;

{for i:=0 to 7 do
begin
TempInt:=Int and 15;
RemainderStr:=Myarray[TempInt]+ RemainderStr;
Int:=Int shr 4;
end;
Result:=RemainderStr;}
end;

Result:=RemainderStr+‘.‘+RemainderStr2 ;
end
else
begin
Int:=Floor(MYDecimal);
Quotient:= Int;
while Quotient>0 do
begin
Quotient:=( Int div 16 ); //
Remainder:= ( Int mod 16 );//
Int:=Quotient;
RemainderStr:=Myarray[Remainder]+ RemainderStr;
end;
Result:=RemainderStr;


{Int:=Floor(MYDecimal);
for i:=0 to 7 do
begin
TempInt:=Int and 15;
RemainderStr:=Myarray[TempInt]+ RemainderStr;
Int:=Int shr 4;
end;
Result:=RemainderStr;
}
end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
if Edit1.Focused then
begin
Label4.Caption :=IntToStr(Length(Trim(Edit1.Text)));
if Edit1.Text <>‘‘ then
begin
if (StrToFloatDef(Edit1.Text,0)>0) and (StrToFloatDef(Edit1.Text,0)<10000000000.0) then // 块俱计计程10
begin
Edit2.Text:=DecimalToHex(StrToFloat(Edit1.Text));
end
else
Edit2.Text:=‘‘;
end
else
Edit2.Text:=‘‘;
end;

end;

procedure TForm1.Edit2Change(Sender: TObject);
var

Edit2_ss,ss,ss_part:string;
//Binbuffer:TByteArray;
HexStr,Binbuffer:PChar;
i:Integer;
begin
if Edit2.Focused then
begin
ss:=‘‘;
ss_part:=‘‘;
Edit2_ss:=trim(Edit2.Text);
if Pos(‘.‘,Edit2_ss)>0 then
begin
ss:=Copy(Edit2_ss,1, Pos(‘.‘,Edit2_ss)-1);
ss_part:=Copy(Edit2_ss,Pos(‘.‘,Edit2_ss),20 ) ;
end
else
ss:=Edit2_ss;

if Length (ss_part)>8 then//计场だ纐粄7硂柑Τ计翴 ┮琌8
Exit;

begin
if High (Myarray_X) begin
Exit;
end
else if High (Myarray_X)=Length(ss) then //7FFFFFFF
begin
if ord(ss[1])>55 then
Exit;
end;

end;
Edit1.Text :=HexToDecimal(ss+ss_part);
end;
end;

function TForm1.HexToDecimal(MYHex: String): string;
var
Hex_Int,HexSub_int,Hex_Char,Hex_part,HexSub_part,HexChar_part:string;
HexPosition_int,HexPosValue_int,HexPosition_part,HexPosValue_part,Curr_Pos:Integer;
ResultValueInt,ResultValuepart:Double;
begin
Edit1.Clear;
Result:=‘0‘;
ResultValueInt:=0;
ResultValuepart:=0;
if Pos(‘.‘,MYHex)>0 then //是否有小数点
begin
Hex_Int:=Copy(MYHex,1,Pos(‘.‘,MYHex)-1);
Hex_part:=Copy(MYHex,Pos(‘.‘,MYHex)+1,7);
end
else
Hex_Int:=MYHex;

if Hex_part<>‘‘ then //小数部分 //小数部分写的怪怪的,哈哈,坑啊,用下面整数的代码改的,小数部分用for循环就好,哈哈
begin
Curr_Pos:=0;//当前位置
HexSub_part:=UpperCase(Hex_part);
HexPosition_part:=Length(HexSub_part);
while Curr_Pos begin
Inc(Curr_Pos);
HexChar_part:=HexSub_part[1];
HexPosValue_part:=ord(HexChar_part[1]);
if (HexPosValue_part>47) and (HexPosValue_part<58) then//(0是48 9是57)
begin
HexPosValue_part:=StrToInt(HexChar_part);
ResultValuepart:=ResultValuepart+ HexPosValue_part*1/Myarray_X[Curr_Pos+1];
end
else if (HexPosValue_part>64) and (HexPosValue_part<71) then//(A是65 F是70)
begin
if HexChar_part=‘A‘ then
begin
HexPosValue_part:=10;
end
else if HexChar_part=‘B‘ then
begin
HexPosValue_part:=11;
end
else if HexChar_part=‘C‘ then
begin
HexPosValue_part:=12;
end
else if HexChar_part=‘D‘ then
begin
HexPosValue_part:=13;
end
else if HexChar_part=‘E‘ then
begin
HexPosValue_part:=14;
end
else if HexChar_part=‘F‘ then
begin
HexPosValue_part:=15;
end;
ResultValuepart:=ResultValuepart+ HexPosValue_part*1/Myarray_X[HexPosition_part+1];
end
else
begin
Result:=‘NaN‘ ;
Exit;
end;
Delete(HexSub_part,1,1);
//HexPosition_part:=Length(HexSub_part);
end;
end;

if Hex_Int<>‘‘ then//整数部分
begin
HexSub_int:=UpperCase(Hex_Int);
HexPosition_int:=Length(HexSub_int);
while HexPosition_int>0 do
begin
Hex_Char:=HexSub_int[1];
HexPosValue_int:=ord(Hex_Char[1]);
if (HexPosValue_int>47) and (HexPosValue_int<58) then//(0是48 9是57)
begin
HexPosValue_int:=StrToInt(Hex_Char);
ResultValueInt:=ResultValueInt+ HexPosValue_int*Myarray_X[HexPosition_int];
end
else if (HexPosValue_int>64) and (HexPosValue_int<71) then//(A是65 F是70)
begin
if Hex_Char=‘A‘ then
begin
HexPosValue_int:=10;
end
else if Hex_Char=‘B‘ then
begin
HexPosValue_int:=11;
end
else if Hex_Char=‘C‘ then
begin
HexPosValue_int:=12;
end
else if Hex_Char=‘D‘ then
begin
HexPosValue_int:=13;
end
else if Hex_Char=‘E‘ then
begin
HexPosValue_int:=14;
end
else if Hex_Char=‘F‘ then
begin
HexPosValue_int:=15;
end;
ResultValueInt:=ResultValueInt+ HexPosValue_int*Myarray_X[HexPosition_int];
end
else
begin
Result:=‘NaN‘ ;
Exit;
end;
Delete(HexSub_int,1,1);
HexPosition_int:=Length(HexSub_int);
end;
end;

Result:=Floattostr(ResultValueInt+ResultValuepart)

end;
end.

-------------Unit-----

 

------------Form---------

object Form1: TForm1
Left = 520
Top = 311
Width = 299
Height = 173
Caption = ‘Form1‘
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = ‘MS Sans Serif‘
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 16
Top = 47
Width = 62
Height = 18
Alignment = taRightJustify
AutoSize = False
Caption = ‘10進制數‘
end
object Label2: TLabel
Left = 14
Top = 101
Width = 63
Height = 13
Alignment = taRightJustify
AutoSize = False
Caption = ‘16進制數‘
end
object Label3: TLabel
Left = 0
Top = 0
Width = 283
Height = 13
Align = alTop
Alignment = taCenter
Caption = ‘整數最多為10個數字‘
end
object Label4: TLabel
Left = 0
Top = 13
Width = 283
Height = 13
Align = alTop
Alignment = taCenter
AutoSize = False
end
object Edit1: TEdit
Left = 79
Top = 43
Width = 183
Height = 21
ImeName = ‘中文(?体) - 搜狗拼音?入法‘
TabOrder = 0
OnChange = Edit1Change
end
object Edit2: TEdit
Left = 81
Top = 98
Width = 182
Height = 21
ImeName = ‘中文(?体) - 搜狗拼音?入法‘
TabOrder = 1
OnChange = Edit2Change
end
end

-----------Form---------

原文:https://www.cnblogs.com/dmqhjp/p/14929769.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐