16进制ff转化为二进制_delphi 进制转换 10进制 2进制 16进制 hex

一、完全用API完成:..uses Windows;

function IntToStr(I: integer): string;

begin

Str(I, Result);

end;

function StrToInt(S: string): integer;

begin

Val(S, Result, Result);

end;

function HexToInt(Const HexValue: String) : Integer;

begin

Val(‘$‘+HexValue, Result, Result);

end;

function IntToHex(Const Value: Integer): string;

const

HexChars: array[0..15] of Char = ‘0123456789ABCDEF‘;

var

iTemp: Integer;

i: Integer;

begin

Result := ‘‘;

i := 0;

while i<4 do

begin

case i of

0: iTemp := Value shr 24 and $FF;

1: iTemp := Value shr 16 and $FF;

2: iTemp := Value shr 8 and $FF;

3: iTemp := Value and $FF;

end;

Result := Result + HexChars[iTemp div 16];

Result := Result + HexChars[iTemp mod 16];

Inc(i);

end;

end;

function LowerCase(const S: string): String;

begin

Result:=CharLower(Pchar(S));

end;

function UpperCase(const S: string): String;

begin

Result:=CharUpper(Pchar(S));

end;

二、函数实现

//十进制 to 二进制

function IntToBin(Value: LongInt;Size: Integer): String;

var

i: Integer;

begin

Result:=‘‘;

for i:=Size-1 downto 0 do begin

if Value and (1 shl i)<>0 then begin

Result:=Result+‘1‘;

end else begin

Result:=Result+‘0‘;

end;

end;

end;

//二进制 to 十进制

function BintoInt(Value:   String):   LongInt;

var

i,Size:   Integer;

begin

Result:=0;

Size:=Length(Value);

for   i:=Size   downto   1   do

begin

if   Copy(Value,i,1)=‘1‘   then

Result:=Result+(1   shl   (Size-i));

end;

end;

function floatBintoInt(Value:   String):   real;

var

i,Size:   Integer;

begin

Result:=0;

Size:=Length(Value);

for   i:=Size   downto   1   do

begin

if   Copy(Value,i,1)=‘1‘   then

Result:=Result+1/(1   shl i);

end;

end;

//十六进制 to 二进制

function HextoBinary(Hex:string):string;

const

BOX: array [0..15] of string =

(‘0000‘,‘0001‘,‘0010‘,‘0011‘,

‘0100‘,‘0101‘,‘0110‘,‘0111‘,

‘1000‘,‘1001‘,‘1010‘,‘1011‘,

‘1100‘,‘1101‘,‘1110‘,‘1111‘);

var

i:integer;

begin

for i:=Length(Hex) downto 1 do

Result:=BOX[StrToInt(‘$‘+Hex[i])]+Result;

end;

//十六进制 to 十进制 浮点型

function HextoFloat(s:string):real;

var b,temp:string;

e:integer;

f:real;

begin

b:=HextoBinary(s);

temp := copy(b,2,8);

e:=BintoInt(temp)-127;

temp := copy(b,10,23);

f := 1+floatBintoInt(temp);

if(copy(b,1,1)=‘0‘)then

result := power(2,e)*f

else

result :=-power(2,e)*f;

end;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值