comp.lang.ada
 help / color / mirror / Atom feed
From: James Rogers <jimmaureenrogers@worldnet.att.net>
Subject: Re: converting - adasockets
Date: Tue, 12 Jun 2001 04:41:00 GMT
Date: 2001-06-12T04:41:00+00:00	[thread overview]
Message-ID: <3B259DF7.FB786846@worldnet.att.net> (raw)
In-Reply-To: q6eV6.1715$yq.619409512@newssvr17.news.prodigy.com

Ken Garlington wrote:
> 
> By the way, I couldn't find anything definitive in the Ada standard that
> says String(1..4) is guaranteed to be 32 bits; although I admit it's usually
> the case. I'd be interested in a description of why this would be considered
> otherwise, since I've used one (Ada83) compiler where this wasn't the case.
> Of course, there is definitely no guarantee that Integer is 32 bits; it
> seems to me that an explicit type definition would be better here...
> 

The Ada standard does not specify the size of an Integer. To do so
would be to make Ada useless for general purpose embedded programming.

A more general approach to memory overlays of strings and integers
is as follows:

-- Coversion between String and Integer when the string merely contains
-- the bit pattern for the integer, not the character representation
-- of the integer.
--
with Ada.Text_Io;

procedure Memory_Overlay is
   Int_Bytes : constant Integer := Integer'Size / Character'Size;
   String_Rep : String(1..Int_Bytes);
   Integer_Rep : Integer;
   Num : Integer;

   for String_Rep'Address use Integer_Rep'Address;
begin
   Num := 0;
   while Num < 1024 loop
      Integer_Rep := Num;
      Ada.Text_Io.Put_Line("Integer_Rep:" & Integer'Image(Integer_Rep));
      Ada.Text_Io.Put_Line("String_Rep: " & String_Rep);
      Num := Num + 100;
   end loop;
   Integer_Rep := Integer'Last;
   Ada.Text_Io.Put_Line("Integer_Rep:" & Integer'Image(Integer_Rep));
   Ada.Text_Io.Put_Line("String_Rep: " & String_Rep);
   Integer_Rep := Integer'First;
   Ada.Text_Io.Put_Line("Integer_Rep:" & Integer'Image(Integer_Rep));
   Ada.Text_Io.Put_Line("String_Rep: " & String_Rep);
   String_Rep := "This";
   Ada.Text_Io.Put_Line("Integer_Rep:" & Integer'Image(Integer_Rep));
   Ada.Text_Io.Put_Line("String_Rep: " & String_Rep);
end Memory_Overlay;

Note that the size of the string is calculated from the sizes of
the Integer type and the Character type. Of course, it is still
possible that a Character is 8 bits and an Integer is 34 bits, 
giving a bad result. The likelyhood of such an occurance is nearly
zero.

I do expect that an integer on a 64 bit machine may actually be
64 bits long. The above calculation will account for such a beast.

Of course, the original poster wanted to convert data received
over a socket connection. In this case the sending and receiving
applications must have a common understanding of the data
representations being transmitted. In that case, I would expect
some static expression such as String(1..4) rather than testing
for the local default size of an integer. I would also expect
an integer definition such as: 

type socket_integer is range -2**32..(2**32 - 1);
for socket_integer'size use 32;

Jim Rogers
Colorado Springs, Colorado USA



  reply	other threads:[~2001-06-12  4:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-06-11  6:50 converting - adasockets 
2001-06-11  9:22 ` Gerald Kasner
2001-06-11 10:17 ` David C. Hoos, Sr.
2001-06-11 11:45   ` Gerald Kasner
2001-06-11 15:59     ` James Rogers
2001-06-11 16:29       ` James Rogers
2001-06-12  1:19         ` Ken Garlington
2001-06-12  4:41           ` James Rogers [this message]
2001-06-11 16:33 ` tmoran
2001-06-18 17:13 ` Stephen Leake
replies disabled

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