ArfIsAToe

codage decodage

Apr 3rd, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 5.58 KB | None | 0 0
  1. program codibb;
  2. uses WinCrt ;
  3. Var
  4.  a,b,c,d,e,n : Integer;
  5.  ch,cc,hc : string ;
  6.  
  7.  Procedure cod(var n : integer );
  8.   var
  9.      a,b,c,d,e : integer;
  10.      ch,cc : string;
  11.     begin
  12.      write('ekteb 3dad : ');
  13.       readln(n);
  14.         ch:='';
  15.         Repeat
  16.          str(n mod 2 , cc );
  17.          ch:=cc+ch;
  18.          n := n div 2 ;
  19.         until n = 0 ;
  20.         Writeln(ch);
  21.     end;
  22.     Procedure deco(var ch :string);
  23.      var
  24.       a,b,c,d,e,n : Integer;
  25.         cc: string ;
  26.      begin
  27.       repeat
  28.          b:=0;
  29.          write('ekteb code binaire : ');
  30.           readln(ch);
  31.          for a:=1 to Length(ch)do
  32.           if (ch[a] <>'0') and (ch[a] <> '1') then
  33.              b:=b+1;
  34.         Until b = 0 ;
  35.         b:=1;
  36.         n:=0;
  37.         for a:= Length(ch) DownTo 1 do
  38.          begin
  39.           if ch[a] = '1' then
  40.              n := n+b;
  41.  
  42.           b := b*2 ;
  43.          end;
  44.  
  45.          writeln(n);
  46.      End;
  47.      procedure chex(var n : integer);
  48.        var
  49.          a,b,c,d,e : Integer;
  50.          cc,ch,hc: String;
  51.         begin
  52.         // decimale -> binaire
  53.            write('ekteb 3dad : ');
  54.               readln(n);
  55.                 ch:='';
  56.                 Repeat
  57.                  str(n mod 2 , cc );
  58.                  ch:=cc+ch;
  59.                  n := n div 2 ;
  60.                 until n = 0 ;
  61.                 writeln(ch);
  62.         // tawa el hexadecimal part
  63.          hc:='';
  64.          for e:=1 to Length(ch) div 4 + 1 do
  65.           begin
  66.                  cc:= copy(ch,Length(ch)-3,4);
  67.                  Delete(ch,Length(ch)-3,4);
  68.                  b:=1;
  69.                  n:=0;
  70.                  for a:=Length(cc) DownTo 1 do
  71.                   begin
  72.                      if cc[a]='1' then
  73.                       n:= n+b;
  74.                      b := b*2;
  75.                     end;
  76.                     if n >= 10 then
  77.                      hc:= chr(n+55) + hc
  78.                     Else
  79.                      begin
  80.                       str(n,cc);
  81.                       hc:=cc+hc;
  82.                      end;
  83.           End;
  84.      write(hc);
  85.         end;
  86.         Procedure dhex(var ch : string);
  87.             var
  88.              a,b,c,d,e,n : Integer;
  89.              cc,hc: String;
  90.                 begin
  91.                  repeat
  92.              b:=0;
  93.              write('ekteb code binaire : ');
  94.               readln(ch);
  95.              for a:=1 to Length(ch)do
  96.               if (ch[a] <>'0') and (ch[a] <> '1') then
  97.                  b:=b+1;
  98.             Until b = 0 ;
  99.              hc:='';
  100.              for e:=1 to (Length(ch) div 4) + 1 do
  101.               begin
  102.                      cc:= copy(ch,Length(ch)-3,4);
  103.                      Delete(ch,Length(ch)-3,4);
  104.                      b:=1;
  105.                      n:=0;
  106.                      for a:=Length(cc) DownTo 1 do
  107.                       begin
  108.                          if cc[a]='1' then
  109.                           n:= n+b;
  110.                          b := b*2;
  111.                         end;
  112.                         if n >= 10 then
  113.                          hc:= chr(n+55) + hc
  114.                         Else
  115.                          begin
  116.                           str(n,cc);
  117.                           hc:=cc+hc;
  118.                          end;
  119.               End;
  120.          write(hc);
  121.             end ;
  122.              procedure indhex(var hc : string);
  123.                 var
  124.                     a,b,c,d,e,n : Integer;
  125.                   cc,ch: String;
  126.  
  127.                 begin
  128.             repeat
  129.                         b:=0;
  130.                     Write('ekteb code Hexadecimal : ');
  131.                         readln(hc);
  132.                         for a:= 1 to Length(hc) do
  133.                          if not(hc[a] in ['1'..'9']) and not (Upcase(hc[a]) in ['A'..'F']) then
  134.                             b:=b+1
  135.                           Else
  136.                               hc[a]:=Upcase(hc[a]);
  137.           Until b=0;
  138.  
  139.                      ch:='';
  140.                      c:=0;
  141.                      cc:='';
  142.                     for a:=1 to Length(hc) do
  143.                      if hc[a] in ['1'..'9'] then
  144.                         begin
  145.                          val(hc[a],c,e);
  146.                          b:=8 ;
  147.                           for e:=1 to 4 Do
  148.                             Begin
  149.                             if c>= b then
  150.                                  begin
  151.                                      c:=c-b;
  152.                                      ch:= ch+'1' ;
  153.                                  end
  154.                                 Else
  155.                                  ch:= ch+'0';
  156.  
  157.                                 b:=b div 2 ;
  158.                             end;
  159.                             ch:=ch+'  ';
  160.                         end
  161.  
  162.                      Else
  163.  
  164.                         begin
  165.                              n := ord(hc[a])-55;
  166.                              b:=8;
  167.                              for e:=1 to 4 Do
  168.                                 Begin
  169.                                 if n>= b then
  170.                                      begin
  171.                                         ch:= ch+'1';
  172.                                         n:=n-b;
  173.                                      end
  174.  
  175.                                     Else
  176.                                      ch:= ch+'0';
  177.  
  178.                                     b:=b div 2 ;
  179.                                 end;
  180.  
  181.                             ch:= ch+'  ';
  182.                         end;
  183.  
  184.                         writeln('ch  ',ch);
  185.                 end;
  186.                 procedure inchex(var hc : string);
  187.                 var
  188.                     a,b,c,d,e,n : Integer;
  189.                   cc,ch: String;
  190.  
  191.                 begin
  192.  
  193.                     repeat
  194.                         b:=0;
  195.                     Write('ekteb code Hexadecimal : ');
  196.                         readln(hc);
  197.                         for a:= 1 to Length(hc) do
  198.                          if not(hc[a] in ['1'..'9']) and not (Upcase(hc[a]) in ['A'..'F']) then
  199.                             b:=b+1
  200.                           Else
  201.                               hc[a]:=Upcase(hc[a]);
  202.  
  203.                     Until b=0;
  204.  
  205.                      ch:='';
  206.                      c:=0;
  207.                      cc:='';
  208.                     for a:=1 to Length(hc) do
  209.                      if hc[a] in ['1'..'9'] then
  210.                         begin
  211.                          val(hc[a],c,e);
  212.                          b:=8 ;
  213.                           for e:=1 to 4 Do
  214.                             Begin
  215.                             if c>= b then
  216.                                  begin
  217.                                      c:=c-b;
  218.                                      ch:= ch+'1' ;
  219.                                  end
  220.                                 Else
  221.                                  ch:= ch+'0';
  222.  
  223.                                 b:=b div 2 ;
  224.                             end;
  225.                         end
  226.  
  227.                      Else
  228.  
  229.                         begin
  230.                              n := ord(hc[a])-55;
  231.                              b:=8;
  232.                              for e:=1 to 4 Do
  233.                                 Begin
  234.                                 if n>= b then
  235.                                      begin
  236.                                         ch:= ch+'1';
  237.                                         n:=n-b;
  238.                                      end
  239.  
  240.                                     Else
  241.                                      ch:= ch+'0';
  242.  
  243.                                     b:=b div 2 ;
  244.                                 end;
  245.                         end;
  246.  
  247.                         b:=1;
  248.                         n:=0;
  249.                         for a:= Length(ch) DownTo 1 do
  250.                          begin
  251.                           if ch[a] = '1' then
  252.                              n := n+b;
  253.  
  254.                           b := b*2 ;
  255.                          end;
  256.  
  257.                          writeln(n);
  258. end;
  259.                          
  260.                            
  261.                                    
  262. begin
  263.  
  264.  Repeat
  265.   ClrScr;
  266.     write('type of input "2" for binary / "10" for decimal / "16" for Hexadecimal : ');
  267.      Readln(e);
  268.  Until (e = 2) or (e= 10 )or (e = 16);
  269.      if e = 2 then
  270.       begin
  271.            Repeat
  272.                   ClrScr;
  273.                     Write('type of result "10" for decimal / "16" for hexadecimal : ');
  274.                      Readln(b);
  275.              Until (b = 16) or (b= 10 );
  276.              if b=10 then
  277.               deco(ch)
  278.              Else
  279.               dhex(ch);
  280.         End
  281.     Else
  282.         if e = 10    then
  283.          begin
  284.               Repeat
  285.                   ClrScr;
  286.                     Write('type of result "2" for binary / "16" for hexadecimal : ');
  287.                      Readln(b);
  288.               Until (b = 16) or (b= 2 );
  289.                 if b = 2 then
  290.                  cod(n)
  291.                 else
  292.                  chex(n);      
  293.          end
  294.     Else
  295.          begin
  296.               Repeat
  297.                   ClrScr;
  298.                     Write('type of result "2" for binary / "10" for decimal : ');
  299.                      Readln(b);
  300.               Until (b = 10) or (b= 2 );
  301.                 if b = 2 then
  302.                  indhex(hc)
  303.                 else
  304.                  inchex(hc);
  305.          end
  306.                
  307.  
  308.  
  309. End.
Add Comment
Please, Sign In to add comment