home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch30_1.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  1.4 KB  |  55 lines

  1.                        -- Chapter 30 - Programming exercise 1
  2. with Text_IO, Unchecked_Conversion;
  3. use Text_IO;
  4.  
  5. procedure CH30_1 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    NUMBER_OF_BYTES : constant := 2;
  11.  
  12.    type CHAR_ARRAY is array(1..NUMBER_OF_BYTES) of CHARACTER;
  13.  
  14.    Split_Array : CHAR_ARRAY;
  15.    Int_Data    : INTEGER;
  16.  
  17.    function Switch_To_Bits is new Unchecked_Conversion(
  18.                                    Source => INTEGER,
  19.                                    Target => CHAR_ARRAY);
  20.  
  21. begin
  22.  
  23.    for Index in 253..260 loop
  24.       Int_Data := Index;
  25.       Split_Array := Switch_To_Bits(Int_Data);
  26.       Put("Int_Data =");
  27.       Put(Int_Data,6);
  28.       Put("   Split_Array =");
  29.       for Count in 1..NUMBER_OF_BYTES loop
  30.          Put(CHARACTER'POS(Split_Array(Count)),4);
  31.       end loop;
  32.       New_Line;
  33.    end loop;
  34.  
  35. end CH30_1;
  36.  
  37.  
  38.  
  39.  
  40. -- Result of Execution
  41.  
  42. -- Int_Data =   253   Split_Array = 253   0
  43. -- Int_Data =   254   Split_Array = 254   0
  44. -- Int_Data =   255   Split_Array = 255   0
  45. -- Int_Data =   256   Split_Array =   0   1
  46. -- Int_Data =   257   Split_Array =   1   1
  47. -- Int_Data =   258   Split_Array =   2   1
  48. -- Int_Data =   259   Split_Array =   3   1
  49. -- Int_Data =   260   Split_Array =   4   1
  50.  
  51. -- Note that the fields are apparently reversed because of the
  52. --   way the bytes are stored in the microprocessor used for this
  53. --   example program.
  54.  
  55.