Algoritmo para Converter Binário para Decimal e Decimal para Binário.
Autor: Josias Dias | Publicado: 2012-08-14T23:36:00-07:00




Olá! estou compartilhando esse algoritmo que desenvolvi somente para conhecimento e desde já esclareço que, caso seja utilizado de forma inadequada, não serei responsabilizado, a finalidade para a qual utilizar este arquivo é de inteira responsabilidade de quem o fizer...



Desenvolvi um ALGORITMO em PASCALZIM para converter essas paradas... Claro, ele cumpre o que promete, basta escolher opção de conversão... ao sair tem minha assinatura, logicamente!!! vejam... compilem e testem, para sair basta digitar um numero fora do contexto BINÁRIO (maior que 1) OU DECIMAL (Maior que 255 (8 bits)) durante a conversão... VLW!!

Download - Pascalzim 5.1.3


Program SistBinarioJopedias;
var
a:array [1..8] of integer;
b:array [1..8] of integer;
nr:array [1..8] of integer;
conv,r,i,j,num:integer;
&nbsp;resp:string;
&nbsp;Begin
&nbsp;j:=0;
&nbsp;a[1]:=1; a[2]:=2;
&nbsp;for i:=2 to 7 do
&nbsp; begin
&nbsp; &nbsp;a[i+1]:=(a[i]*2);
&nbsp; end;
&nbsp; writeln ('Escolha que tipo de Conversão Deseja: ');
&nbsp; writeln;
&nbsp; writeln ('(1) - Binário/Decimal ou');// ou (2) - Decimal/Binário');
&nbsp; writeln ('(2) - Decimal/Binário');
&nbsp; read (conv);
&nbsp; while (conv=1) do
&nbsp; begin
&nbsp; &nbsp;if (conv=1) then
&nbsp; &nbsp;begin
&nbsp; &nbsp; for i:=1 to 8 do
&nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp;writeln ('Digite o ',i,'o Numero: (BINARIO).');
&nbsp; &nbsp; &nbsp;read (nr[i]);
&nbsp; &nbsp; &nbsp;if (nr[i]&gt;1) then
&nbsp; &nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp; &nbsp;conv:=3;i:=8;
&nbsp; &nbsp; &nbsp; end;
&nbsp; &nbsp; end;
&nbsp; &nbsp; //for i:=1 to 8 do
&nbsp; 
&nbsp; &nbsp; r:=0; j:=1;
&nbsp; &nbsp; for i:=8 downto 1 do
&nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp;if (nr[j]=1) then
&nbsp; &nbsp; &nbsp;begin
  r:=r+a[i];
&nbsp; &nbsp; &nbsp; j:=j+1;
 end
 else if (nr[j]=0) then
 begin
  r:=r+0;
  j:=j+1;
 end;
&nbsp; &nbsp; end;
&nbsp; &nbsp;end;
&nbsp; &nbsp;write ('DECIMAL: ');
&nbsp; &nbsp;writeln(r);
&nbsp; &nbsp;writeln;
&nbsp; end;

&nbsp; while (conv=2) do
&nbsp; begin
&nbsp; if (conv=2) then
&nbsp; &nbsp;begin
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;writeln ('Digite o Numero: (DECIMAL).');
&nbsp; &nbsp; &nbsp;read(num);
&nbsp; 
&nbsp; &nbsp; if (num&gt;255) then
&nbsp; &nbsp; begin
&nbsp; &nbsp; &nbsp;writeln ('ERRO! O numero digitado está fora dos limites (8 bits)');
&nbsp; &nbsp; &nbsp;conv:=3;
&nbsp; &nbsp; end
 else if (num&lt;=255) then
&nbsp; &nbsp; for i:=8 downto 1 do
&nbsp; &nbsp; &nbsp;begin
&nbsp; 
&nbsp; &nbsp; &nbsp;if (num &gt;=a[i]) then
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; num:=num-a[i];
&nbsp; &nbsp; &nbsp; b[i]:=1;
&nbsp; &nbsp; &nbsp;end
&nbsp; &nbsp; &nbsp;else if (num&lt;a[i]) then
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; b[i]:=0;
&nbsp; &nbsp; &nbsp;end;
&nbsp; &nbsp; 
&nbsp; &nbsp; end;
&nbsp; &nbsp; writeln;
&nbsp; &nbsp; write('BINARIO: ');
&nbsp; &nbsp; for i:=8 downto 1 do
&nbsp; &nbsp; &nbsp;begin
&nbsp; &nbsp; &nbsp; write (b[i],' ');
&nbsp; &nbsp; &nbsp;end;
&nbsp; &nbsp; writeln;
&nbsp; &nbsp; writeln;
&nbsp; &nbsp; r:=0; j:=1;
&nbsp; &nbsp; 
&nbsp; &nbsp;end;
&nbsp; 
&nbsp; end;
&nbsp; if conv=3 then
&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Final da Execução!');

&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# &nbsp;#');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # ');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp;# &nbsp; ### &nbsp; #### &nbsp; &nbsp;### &nbsp; &nbsp;#### &nbsp;# &nbsp;#### &nbsp; &nbsp;####');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp;# &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; &nbsp; &nbsp;# &nbsp;# &nbsp; &nbsp;');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp;# &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;##### &nbsp;# &nbsp; # &nbsp;# &nbsp;##### &nbsp;#### ');
&nbsp; writeln(' # &nbsp; &nbsp;# &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; &nbsp; &nbsp;# &nbsp; # &nbsp;# # &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln(' # &nbsp; &nbsp;# &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# &nbsp; # &nbsp;# # &nbsp; &nbsp;# &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln(' ###### &nbsp; ### &nbsp; #### &nbsp; &nbsp;### &nbsp; &nbsp;#### &nbsp;# &nbsp;#### # #### &nbsp;');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln(' &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#');
&nbsp; writeln;
&nbsp;End.

Valeu galera! até a proxima...
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Josias Dias