: can anyone help me with this binary to decimal numbers then another
: one is decimal to binary tnx plus if you know something about binary
: to octal please help thanks
:
Decimal to binary ( and vice versa ) + binary to octal ( and vice versa ) demo (not the nicest code but works):
program dec_bin_oct;
const po2:array[0..7] of byte=(1,2,4,8,16,32,64,128); {powers of 2}
bin:array[false..true] of char='01';
oct:array[0..7] of char='01234567';
{Decimal to binary}
function dec2bin(b:byte):string;
var s:string;
i:byte;
begin
s:='';
for i:=7 downto 0 do
s:=s+bin[(po2[i] and b=po2[i])];
dec2bin:=s;
end;
{Binary ot decimal}
function bin2dec(s:string):byte;
var l:byte absolute s; {lenght of s}
i,b:byte;
begin
b:=0;
while l<8 do s:=bin[false]+s;
if l>8 then s:=copy(s,l-7,8);
for i:=1 to l do
if s[i]=bin[true] then b:=b+po2[8-i];
bin2dec:=b;
end;
{Binary to octal}
function bin2oct(b:string):string;
var s:string;
l:byte absolute b; {lenght of b}
i:byte;
begin
while ((l mod 3)<>0) do b:=bin[false]+b;
s:='';
for i:=1 to (l div 3) do
s:=s+oct[bin2dec(b[pred(i)*3+1]+b[pred(i)*3+2]+b[pred(i)*3+3])];
bin2oct:=s;
end;
{Octal to binary}
function oct2bin(b:string):string;
var l:byte absolute b;
i:byte;
s:string;
begin
s:='';
for i:=1 to l do
s:=s+copy(dec2bin(ord(b[i])-48),6,3);
oct2bin:=s;
end;
var n:byte;
begin
write(#13#10,'Enter a decimal :');readln(n);
writeln(n,' in binary :',dec2bin(n),', in decimal :',bin2dec(dec2bin(n)));
writeln(n,' in octal :',bin2oct(dec2bin(n)),' in binary: ',oct2bin(bin2oct(dec2bin(n))));
readln;
end.