这是我自己写的函数:
function Conv(s:string):string;
var
i,j,k:integer;
begin
Result:='';
i:=length(s);
if (i mod 4)>0 then
for j:=4-(i mod 4) downto 1 do
s:='0'+s;
i:=length(s) div 4;
for j:=0 to i-1 do
begin
k:=(ord(s[j*4+1])-$30)*8+(ord(s[j*4+2])-$30)*4+(ord(s[j*4+3])-$30)*2+(ord(s[j*4+4])-$30);
k:=k+$30;
if k>$39 then k:=k+7;
Result:=Result+chr(k);
end;
end;
function Conv(s:string):string;
var
i,j,k:integer;
begin
Result:='';
i:=length(s);
if (i mod 4)>0 then
for j:=4-(i mod 4) downto 1 do
s:='0'+s;
i:=length(s) div 4;
for j:=0 to i-1 do
begin
k:=(ord(s[j*4+1])-$30)*8+(ord(s[j*4+2])-$30)*4+(ord(s[j*4+3])-$30)*2+(ord(s[j*4+4])-$30);
k:=k+$30;
if k>$39 then k:=k+7;
Result:=Result+chr(k);
end;
end;
function strtobit(str:string):string;
var
i,num:integer;
int:integer;
begin
int:=0;
num:=length(str);
for i:=1 to num do
begin
if copy(str,i,1)='1' then
int:=int+(($01) shl (num-i));
end;
strtobit:=inttohex(int,2);
end;
你的程序只能转换8位以内的字串,最后应改为strtobit:=inttohex(int,(num div 4));
才能完成长串的转换。
function BinToInt( const str: String ): Integer;
var
i, nLen: Integer;
begin
Result := 0;
nLen := Length(str);
Assert( nLen>0 );
for i:=1 to nLen do
begin
Assert( str[i] in ['0', '1'] );
Result := Result shl 1 + Ord(str[i]='1');
end;
end;怎么样,最省吧?