--  Reads integers, swaps nibbles, and extracts and prints characters
--  Uses a packed array of Nibbles to swap the Nibbles

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; 
with Ada.Unchecked_Conversion;
 
procedure decoding2b  is 
   type bit is mod 2;

   type Nibble is mod 2 ** 4;
   type Nibble_Array is array(Natural range <>) of Nibble with pack;
   subtype Nibble_Array8 is Nibble_Array(1 .. 8);

   subtype String4 is String(1 .. 4);

   function rev(s: String) return String is
      ans: String(s'range);
   begin
      for i in s'range loop
         ans(i) := s(s'first + s'last - i);  -- last shall be first
      end loop;
      return ans;
   end rev;

   function swap_nibbles(n: Nibble_Array8) return Nibble_Array8 is
      ( n(2) & n(1) & n(4) & n(3) & n(6) & n(5) & n(8) & n(7));

   function int_to_Nibble_Array8 is new ada.Unchecked_Conversion
      (source => Integer, target => Nibble_Array8);
   function Nibble_Array8_to_string4 is new ada.Unchecked_Conversion
      (source => Nibble_Array8, target => String4);

   i: Integer;
   m, n: Nibble_Array8;
   s: String4;
begin
   while not end_of_file loop
      get(i);

      m := int_to_Nibble_Array8(i);
      n := swap_nibbles(m);
      s := Nibble_Array8_to_string4(n);

      put(rev(s));
   end loop;
end decoding2b;