二进制与十进制的转换,请帮忙!全文检索无法使用。(100分)

  • 主题发起人 主题发起人 shamohu
  • 开始时间 开始时间
S

shamohu

Unregistered / Unconfirmed
GUEST, unregistred user!
如何进行二进制与十进制间的相互转换?有无专用的函数?比如将-23.569812或8.11转换成二进制,反之亦然。谢谢!<br><br>(全文检索无法使用,总出错)
 
修改一下<br><br><br><br>function octtobin(i:integer):string; &nbsp; {十进制转换为二进制函数}<br>var<br>&nbsp; j:integer;<br>&nbsp; s:string;<br>begin<br>&nbsp; j:=i;<br>&nbsp; s:=' ';<br>&nbsp; &nbsp; while j&gt;=2 do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;if (j mod 2)=1 then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:='1'+s;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=j div 2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:='0'+s;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j:=j div 2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;s:=chr(ord('0')+j) + s;<br>&nbsp; //octtobin := s;//删除此句<br>&nbsp; &nbsp;octtobin := Trim(s);//修改支出<br><br>end;<br><br>function bintooct(k:string):integer; &nbsp; &nbsp; &nbsp; {二进制转换为十进制函数}<br>var<br>&nbsp; i,j,t:integer;<br>&nbsp; s:char;<br>begin<br>&nbsp; &nbsp; &nbsp;t:=1;<br>&nbsp; &nbsp; &nbsp;j:=length(k);<br>&nbsp; &nbsp; &nbsp;j:=0+(ord(k[j])-ord('0'))*t;<br>&nbsp; &nbsp; &nbsp;for i:=length(k)-1 downto 1 do<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:=k;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; t:=t*2;<br>&nbsp; &nbsp;j:=j + ((ord(s)-ord('0'))*t);<br>end;<br>&nbsp; &nbsp; &nbsp; &nbsp; bintooct:=j;<br>&nbsp; &nbsp; &nbsp;k := Trim(k);//删除此句<br>&nbsp; &nbsp;<br>end;<br>
 
delphi中有提供专用的函数,我用过好像在IdGlobal.pas中
 
TO "不知道_0909": 你提供的函数只能在整数与二进制的转换,而我所需要的是实数(包括正负)与二进制的转换。<br>TO ”Supermay“:我用的是Delphi 5,没有发现有IdGlobal.pas文件。<br><br>谢谢二位!望继续帮忙!<br>
 
这是我改写的带小数位的,正负的没有写,你改一下就可以了<br>function octtobin1(i:Extended):string; &nbsp; {十进制转换为二进制函数}<br>var<br>&nbsp;j,k: integer;<br>&nbsp;m1,m2: Extended;<br>&nbsp;s1,s2:string;<br>begin<br>&nbsp; j := trunc(i);<br>&nbsp; s1:=' ';<br>&nbsp; s2:=' ';<br>&nbsp; while j&gt;=2 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (j mod 2) =1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s1:='1'+s1;<br>&nbsp; &nbsp; &nbsp; j:=j div 2;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;s1:='0'+s1;<br>&nbsp; &nbsp; &nbsp; &nbsp;j:=j div 2;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; s1:=chr(ord('0')+j) + s1;<br>&nbsp; octtobin1 := Trim(s1);<br>&nbsp; if trunc(i)&lt;&gt; i then<br>&nbsp; begin<br>&nbsp; &nbsp; m1:= i-j;<br>&nbsp; &nbsp; k := length(floattostr(m1))-2;<br>&nbsp; &nbsp; m2 := m1 * power(10,k);<br>&nbsp; &nbsp; k := trunc(m2);<br>&nbsp; &nbsp; while k&gt;=2 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (k mod 2) =1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; s2:='1'+s2;<br>&nbsp; &nbsp; &nbsp; &nbsp; k:=k div 2;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s2:='0'+s2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k:=k div 2;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; s2:=chr(ord('0')+k) + s2;<br>&nbsp; &nbsp; octtobin1 := Trim(s1 + '.' + s2);<br>&nbsp; end;<br>end;<br>
 
这次应该能满足你的需要<br>function bintooct(k:string):string; &nbsp; &nbsp; &nbsp; {二进制转换为十进制函数}<br>var<br>&nbsp;i,j,t:integer;<br>&nbsp;s:char;<br>&nbsp;s1,s2,s3: string;<br>begin<br>&nbsp; if leftstr(trim(k),1) = '0' then<br>&nbsp; &nbsp; s1 := ''<br>&nbsp; else<br>&nbsp; &nbsp; s1 := '-';<br>&nbsp; k := rightstr(trim(k),length(k)-1);<br>&nbsp; if pos('.',k) = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; t:=1;<br>&nbsp; &nbsp; j:=length(k);<br>&nbsp; &nbsp; j:=0+(ord(k[j])-ord('0'))*t;<br>&nbsp; &nbsp; for i:=length(k)-1 downto 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s:=k;<br>&nbsp; &nbsp; &nbsp; t:=t*2;<br>&nbsp; &nbsp; &nbsp; j:=j + ((ord(s)-ord('0'))*t);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; bintooct:=s1 +inttostr(j);<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin//带有小数位的<br>&nbsp; &nbsp; t:=1;<br>&nbsp; &nbsp; s2 := leftstr(k, pos('.',k)-1);<br>&nbsp; &nbsp; j:=length(s2);<br>&nbsp; &nbsp; j:=0+(ord(s2[j])-ord('0'))*t;<br>&nbsp; &nbsp; for i:=length(s2)-1 downto 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s:=s2;<br>&nbsp; &nbsp; &nbsp; t:=t*2;<br>&nbsp; &nbsp; &nbsp; j:=j + ((ord(s)-ord('0'))*t);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; s3 := inttostr(j);<br><br>&nbsp; &nbsp; t:=1;<br>&nbsp; &nbsp; s2 := trim(copy(k, pos('.',k)+1,length(k)));<br>&nbsp; &nbsp; j:=length(s2);<br>&nbsp; &nbsp; j:=0+(ord(s2[j])-ord('0'))*t;<br>&nbsp; &nbsp; for i:=length(s2)-1 downto 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s:=s2;<br>&nbsp; &nbsp; &nbsp; t:=t*2;<br>&nbsp; &nbsp; &nbsp; j:=j + ((ord(s)-ord('0'))*t);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; bintooct:=s1 +s3+'.'+inttostr(j);<br>&nbsp; end;<br>end;<br><br>function octtobin(i:Extended):string; &nbsp; {十进制转换为二进制函数}<br>var<br>&nbsp;j,k: integer;<br>&nbsp;m1,m2: Extended;<br>&nbsp;s1,s2, s3, s4:string;<br>begin<br>&nbsp; if i &gt;= 0 then<br>&nbsp; &nbsp; s3 := &nbsp;'0'<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; i := -i ;<br>&nbsp; &nbsp; s3 := '1';<br>&nbsp; end;<br>&nbsp; j := trunc(i);<br>&nbsp; s1:=' ';<br>&nbsp; s2:=' ';<br>&nbsp; while j&gt;=2 do<br>&nbsp; begin<br>&nbsp; &nbsp; if (j mod 2) =1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; s1:='1'+s1;<br>&nbsp; &nbsp; &nbsp; j:=j div 2;<br>&nbsp; &nbsp; end<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp;s1:='0'+s1;<br>&nbsp; &nbsp; &nbsp; &nbsp;j:=j div 2;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; s1:=chr(ord('0')+j) + s1;<br>&nbsp; s4 := Trim(s3+s1);<br>&nbsp; if trunc(i)&lt;&gt; i then<br>&nbsp; begin<br>&nbsp; &nbsp; s2 := '';<br>&nbsp; &nbsp; m1:= i-trunc(i);<br>&nbsp; &nbsp; k := length(floattostr(m1))-2;<br>&nbsp; &nbsp; m2 := m1 * power(10,k);<br>&nbsp; &nbsp; k := trunc(m2);<br>&nbsp; &nbsp; while k&gt;=2 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if (k mod 2) =1 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; s2:='1'+s2;<br>&nbsp; &nbsp; &nbsp; &nbsp; k:=k div 2;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;s2:='0'+s2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;k:=k div 2;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; s2:=chr(ord('0')+k) + s2;<br>&nbsp; &nbsp; s4 := Trim(s4 + '.' + s2);<br>&nbsp; end;<br>&nbsp; octtobin := s4;<br>end;
 
谢谢“不知道_0909”!
 
二进制转换为十进制函数<br>11001100.11001<br>1*2^7 + 1*2^6 + 0*2^5 + 0*2^4 + 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0 + 1*2^(-1) <br>+ 1*2^(-2) + 0*2^(-3) + 0*2^(-4) + 1*2^(-5)
 
l12004的方法更易理解啊<br>将二进制的串一个一个取出来再乘以它的权就可以了
 

Similar threads

回复
0
查看
873
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部