二进制字符串与十进制的转换
此函数没有查到,好象Delphi未提供,本算法根据其转换方法而写:
代码
procedure TForm1.FormCreate(Sender: TObject);
var
v:string;
i,j:Integer;
k:Integer;
begin
//二进制字符串转十进制
v:='100101001'; //297
j:=0;
k:=0;
for i:=Length(v) downto 1 do
begin
if v[i]='1' then
k:=k+power(2, j);
j:=j+1;
end;
//ShowMessage(FloatToStr(k));
//十进制转二进制字符串
k:=299; //100101011
v:='';
while k>1 do
begin
v:=IntToStr(k mod 2)+v;
k:=k div 2;
end;
v:=IntToStr(k)+v;
//ShowMessage(v);
end;
var
v:string;
i,j:Integer;
k:Integer;
begin
//二进制字符串转十进制
v:='100101001'; //297
j:=0;
k:=0;
for i:=Length(v) downto 1 do
begin
if v[i]='1' then
k:=k+power(2, j);
j:=j+1;
end;
//ShowMessage(FloatToStr(k));
//十进制转二进制字符串
k:=299; //100101011
v:='';
while k>1 do
begin
v:=IntToStr(k mod 2)+v;
k:=k div 2;
end;
v:=IntToStr(k)+v;
//ShowMessage(v);
end;