Delphi实现把10进制转换成16进制的函数进制转化
2013-04-15 14:21:22 来源:WEB开发网vD := 0;
end;
end;
if I > 0then
begin
case vD of
0..9 : vHextStr[vLen] := Chr(vD + $30);
10..15 : vHextStr[vLen] := Chr(vD + $41);
end;
end;
Result := vHextStr;
end;
function TConversion.BitStrToOStr(const BitStr: String): String;
var
vD : Byte;
I : Integer;
vHextStr : String;
vP : PChar;
vLen : Integer;
begin
vLen := Length(BitStr);
if vLen mod 3 > 0 then
begin
SetLength(vHextStr, vLen div 3 + 1);
vLen := vlen div 3 + 1;
end
else
begin
SetLength(vHextStr, vLen div 3);
vLen := vlen div 3 ;
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;
end;
end;
Dec(vP);
Inc(I);
if I = 3 then
begin
case vD of
0..9 : vHextStr[vLen] := Chr(vD + $30);
end;
Dec(vLen);
I := 0;
vD := 0;
end;
end;
if I > 0then
begin
case vD of
0..9 : vHextStr[vLen] := Chr(vD + $30);
end;
end;
Result := vHextStr;
end;
function TConversion.HexToBitStr(HexStr: string): string;
const
cBitStrings: 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
Result := '';
for I := 1 to Length(HexStr) do
Result := Result + cBitStrings[StrToIntDef('$' + HexStr[I], 0)];
while Pos('0', Result) = 1 do Delete(Result, 1, 1);
end; { HexToBit }
function TConversion.HextoIntStr(HexStr: String): string;
begin
result:= IntToStr(StrToInt('$' + (HexStr)));
end;
function TConversion.HexToOStr(HexStr: string): string;
begin
Result := BitStrToOStr(HexToBitStr(HexStr));
end;
function TConversion.inttoBitStr(intstr: string): string;
var
i :Integer;
begin
i := StrToInt(intstr);
while i <>0 do
begin //i mod 2取模,再使用format格式化
result:=Format('%d'+result,[i mod 2]);
i:=i div 2
end
end;
//10进制装换 2进制 第二种方法
{function TConversion.IntToBitStr(Value, 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;}
function TConversion.IntToHexStr(intStr: string): String;
begin
Result:=inttoBitStr(intstr);
end;
function TConversion.IntToOStr(intstr: string): string;
begin
Result := BitStrToHextStr(inttoBitStr(intstr));
end;
function TConversion.OtoBitStr(O: string): string;
更多精彩
赞助商链接