WEB开发网
开发学院软件开发Delphi Delphi实现把10进制转换成16进制的函数进制转化 阅读

Delphi实现把10进制转换成16进制的函数进制转化

 2013-04-15 14:21:22 来源:WEB开发网   
核心提示: delphi中有直接把10进制转换成16进制的函数:function IntToHex(Value: Integer; Digits: Integer): string; overload; function IntToHex(Value: Int64; Digits: In

 delphi中有直接把10进制转换成16进制的函数:

function IntToHex(Value: Integer; Digits: Integer): string; overload;
function IntToHex(Value: Int64; Digits: Integer): string; overload;

unit uConversion;


interface
uses
SysUtils,Math;
type
TConversion = class
public
//10 进制 to 2,8,16 进制
function inttoBitStr(intstr: string): string;
function IntToHexStr(intStr: string): String;//10 = 2
function IntToOStr(intstr : string): string;

//2进制 to 10,8,16 进制
function BittoInt(BitStr: String): LongInt;// 2 = 10
function BitStrToHextStr(const BitStr : String) : String;//2 = 16
function BitStrToOStr(const BitStr : String) : String;//2 = 8

//16 > 10 2 8 进制
function HextoIntStr(HexStr: String): string;
function HexToBitStr(HexStr: string): string;
function HexToOStr(HexStr: string): string;

//八进制转换成二进制字符串
function OtoBitStr(O : string):string;
function OtoIntStr(O : string):string;
function OtoHexStr(O : string):string;
end;
var
TC :TConversion;


implementation

{ TConversion }
//2 进制 to 10 进制

function TConversion.BittoInt(BitStr: String): LongInt;
var
i,Size: Integer;
begin
Result:=0;
Size:=Length(BitStr);
for i:=Size downto 1 do
begin
//例如 1010
if Copy(BitStr,i,1)='1' then
Result:=Result+(1 shl (Size-i));
end;
//第二种方法
//二进制转换为十进制 start
{
VAR
str : String;
Int : Integer;
i : integer;

Str := UpperCase(Edit1.Text);
Int := 0;
FOR i := 1 TO Length(str) DO
Int := Int * 2 + ORD(str[i]) - 48;
Edit2.Text:=IntToStr(int);
}
//二进制转换为十进制 end;
//第三中方法
{
function hextoint(s: string): Double;
begin
while Length(s) <>0 do
begin //2^(长度-1)次方
if s[1]='1' then Result:=Result+power(2,Length(s)-1);
s:=Copy(s,2,Length(s));
end
end;
}
end;

function TConversion.BitStrToHextStr(const BitStr: String): String;
var
vD : Byte;
I : Integer;
vHextStr : String;
vP : PChar;
vLen : Integer;
begin
vLen := Length(BitStr);
if vLen mod 4 > 0 then
begin
SetLength(vHextStr, vLen div 4 + 1);
vLen := vlen div 4 + 1;
end
else
begin
SetLength(vHextStr, vLen div 4);
vLen := vlen div 4 ;
end;

//初始化
vD := 0;
vP := PChar(BitStr)+length(BitStr)-1;
I := 0; //开始计数

while vP^ <> #0 do
begin
if vp^ = '1' then
begin
case i of
0: vD :=vd+1;
1: vD :=vd+2;
2: vD :=vd+4;
3: vD :=vd+8;
end;
end;

Dec(vP);
Inc(I);
if I = 4 then
begin
case vD of
0..9 : vHextStr[vLen] := Chr(vD + $30);
10..15 : vHextStr[vLen] := Chr(vD - 10 + $41);
end;
Dec(vLen);

1 2 3  下一页

Tags:Delphi 实现 进制

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接