Guest User

Untitled

a guest
Aug 16th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.39 KB | None | 0 0
  1. program dec_bin_oct;
  2.  
  3. const po2:array[0..7] of byte=(1,2,4,8,16,32,64,128); {powers of 2}
  4.       bin:array[false..true] of char='01';
  5.       oct:array[0..7] of char='01234567';
  6.  
  7. {Decimal to binary}
  8. function dec2bin(b:byte):string;
  9.  var s:string;
  10.      i:byte;
  11.  begin
  12.   s:='';
  13.   for i:=7 downto 0 do
  14.    s:=s+bin[(po2[i] and b=po2[i])];
  15.   dec2bin:=s;
  16.  end;
  17.  
  18. {Binary ot decimal}
  19. function bin2dec(s:string):byte;
  20.  var l:byte absolute s; {lenght of s}
  21.      i,b:byte;
  22.  begin
  23.   b:=0;
  24.   while l<8 do s:=bin[false]+s;
  25.   if l>8 then s:=copy(s,l-7,8);
  26.   for i:=1 to l do
  27.   if s[i]=bin[true] then b:=b+po2[8-i];
  28.   bin2dec:=b;
  29.  end;
  30.  
  31. {Binary to octal}
  32. function bin2oct(b:string):string;
  33.  var s:string;
  34.      l:byte absolute b; {lenght of b}
  35.      i:byte;
  36.  begin
  37.   while ((l mod 3)<>0) do b:=bin[false]+b;
  38.   s:='';
  39.   for i:=1 to (l div 3) do
  40.    s:=s+oct[bin2dec(b[pred(i)*3+1]+b[pred(i)*3+2]+b[pred(i)*3+3])];
  41.   bin2oct:=s;
  42.  end;
  43.  
  44. {Octal to binary}
  45. function oct2bin(b:string):string;
  46.  var l:byte absolute b;
  47.      i:byte;
  48.      s:string;
  49.  begin
  50.   s:='';
  51.   for i:=1 to l do
  52.    s:=s+copy(dec2bin(ord(b[i])-48),6,3);
  53.   oct2bin:=s;
  54.  end;
  55.  
  56.  
  57. var n:byte;
  58.  
  59. begin
  60.  write(#13#10,'Enter a decimal :');readln(n);
  61.  writeln(n,' in binary :',dec2bin(n),', in decimal :',bin2dec(dec2bin(n)));
  62.  writeln(n,' in octal :',bin2oct(dec2bin(n)),' in binary: ',oct2bin(bin2oct(dec2bin(n))));
  63.  readln;
  64. end.
Add Comment
Please, Sign In to add comment