comp.lang.ada
 help / color / mirror / Atom feed
* Ada Recursion with strings
@ 2008-09-30 20:30 Joe
  2008-09-30 22:24 ` Adam Beneschan
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Joe @ 2008-09-30 20:30 UTC (permalink / raw)


Hey all,
I'm trying to write a function that converts a 2**32 mod type into an
string formatted like an IP address (i.e. 192.168.2.1).

Here's what I wrote

with Ada.Text_IO;
use  Ada.Text_IO;

procedure Ip_packet is
   type Word_Type is mod 2**32;

   function IP_String (Word : Word_Type;
                       IP_String : String := "") return String
is
      Sub_String : String := Word_Type'Image(Word mod 2**8);
   begin
      if Word <= 2**8 then
         return Word_Type'Image(Word) & IP_String;
      else
         return IP_String( Word / 2**8,
                           "." & Sub_String & IP_String);
      end if;
   end IP_String;

begin  --  Ip_packet
   Put(IP_String(123456));
end Ip_packet;

There are 2 errors with this that I don't understand.  First, Ada
expects an integer for (word / 2**8).  Is this because word_type is
outside the standard integer range?  How do I get a division operator
that works on word_type?

If I patch the integer error by type casting word to an integer I get
another error.  On the second return statement, Ada complains that
there are "too many subscripts in array reference".  Is there a
limitation on the string I can pass to a recursive function call?

Thanks,
Joe



^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2008-10-03  9:06 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-30 20:30 Ada Recursion with strings Joe
2008-09-30 22:24 ` Adam Beneschan
2008-09-30 22:24 ` Jeffrey R. Carter
2008-10-01  0:13   ` Joe
2008-10-01  0:27     ` Jeffrey R. Carter
2008-10-01  1:15       ` Robert A Duff
2008-10-01 11:34         ` Jeffrey R. Carter
2008-10-01 14:29           ` Adam Beneschan
2008-10-01 15:18             ` Jean-Pierre Rosen
2008-10-01 16:47               ` Georg Bauhaus
2008-10-02  7:24               ` Niklas Holsti
2008-10-02  8:18                 ` Ludovic Brenta
2008-10-03  6:54             ` Maciej Sobczak
2008-10-03  7:38               ` Jean-Pierre Rosen
2008-10-03  9:06               ` Dmitry A. Kazakov
2008-10-01  0:28     ` Adam Beneschan
2008-10-01  9:48       ` Georg Bauhaus
2008-10-01 10:22         ` Dmitry A. Kazakov
2008-10-01  3:01 ` anon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox