comp.lang.ada
 help / color / mirror / Atom feed
* converting - adasockets
@ 2001-06-11  6:50 
  2001-06-11  9:22 ` Gerald Kasner
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From:  @ 2001-06-11  6:50 UTC (permalink / raw)


Hello

Im writting a program which reads some data from a socket 
(i used adasockets). And i've problem with converting
this date from String to fg Integer. How could i do that.
In C it woul look like:

 char s[10];
 int i;

 i = *(int*)s;

but in Ada?

thanks in advance
Godfryd



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

* Re: converting - adasockets
  2001-06-11  6:50 converting - adasockets 
@ 2001-06-11  9:22 ` Gerald Kasner
  2001-06-11 10:17 ` David C. Hoos, Sr.
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Gerald Kasner @ 2001-06-11  9:22 UTC (permalink / raw)


""Micha� Nowikowski\" \"" schrieb:
> 
> Hello
> 
> Im writting a program which reads some data from a socket
> (i used adasockets). And i've problem with converting
> this date from String to fg Integer. How could i do that.
> In C it woul look like:
> 
>  char s[10];
>  int i;
> 
>  i = *(int*)s;
> 
> but in Ada?
> 
> thanks in advance
> Godfryd

look for a book or a tutorial:

http://goanna.cs.rmit.edu.au/~dale/ada/aln/4_basic_types.html#RTFToC13



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

* Re: converting - adasockets
  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 16:33 ` tmoran
  2001-06-18 17:13 ` Stephen Leake
  3 siblings, 1 reply; 10+ messages in thread
From: David C. Hoos, Sr. @ 2001-06-11 10:17 UTC (permalink / raw)


Assuming the following Ada declarations,
S : String [1 .. 10];
I : Integer;

first you would need to declare a String subtype having the same
length as the size of an Integer -- something like this:

subtype Integer_Sized_String is String (1 .. Integer'size / Character'Size);
:
Then you would need to declare an unchecked conversion function,
something like this:

with Ada.Unchecked_Conversion;

function To_Integer is new Ada.Unchecked_Conversion
(Source => Integer_Sized_String,
 Target  => Integer);

With these additional declarations, you can now do the conversion
like this:

I := To_Integer (S (1 .. Integer_Sized_String'Length));

Now that this is done, I have a question for you -- assuming that
sizeof (int) on your machine is 4, what are you going to do with
the additional six characters of s?

You may find it more useful to use the adasockets subprograms
using Stream_Element_Array parameters instead of the
string-oriented ones, because of their special treatment of CR and
LF characters.

"Micha� Nowikowski" <godfryd@zamek.gda.pl> wrote in message
news:20010611.085009.1789366143.627@zamek.gda.pl...
> Hello
>
> Im writting a program which reads some data from a socket
> (i used adasockets). And i've problem with converting
> this date from String to fg Integer. How could i do that.
> In C it woul look like:
>
>  char s[10];
>  int i;
>
>  i = *(int*)s;
>
> but in Ada?
>
> thanks in advance
> Godfryd



-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



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

* Re: converting - adasockets
  2001-06-11 10:17 ` David C. Hoos, Sr.
@ 2001-06-11 11:45   ` Gerald Kasner
  2001-06-11 15:59     ` James Rogers
  0 siblings, 1 reply; 10+ messages in thread
From: Gerald Kasner @ 2001-06-11 11:45 UTC (permalink / raw)


"David C. Hoos, Sr." schrieb:
> 
> Assuming the following Ada declarations,
> S : String [1 .. 10];

This is not an Ada declaration ! (Pascal,Modula-2,Oberon-2...??)

> I : Integer;
> 
Why unchecked conversion ?
what about this ?
#####################
with Ada.Text_IO;
use Ada.Text_IO;
 
procedure example is
 
S : String (1 .. 10);
I : Integer;
 
begin
S:="        10";
I:=Integer'value(S);
 
 
put(Integer'image(i));
New_Line;
 
S:="     -1000";
I:=Integer'value(S);
 
 
put(Integer'image(i));
New_Line;
end example;

-Gerald



> first you would need to declare a String subtype having the same
> length as the size of an Integer -- something like this:
> 
> subtype Integer_Sized_String is String (1 .. Integer'size / Character'Size);
> :
> Then you would need to declare an unchecked conversion function,
> something like this:
> 
> with Ada.Unchecked_Conversion;
> 
> function To_Integer is new Ada.Unchecked_Conversion
> (Source => Integer_Sized_String,
>  Target  => Integer);
> 
> With these additional declarations, you can now do the conversion
> like this:
> 
> I := To_Integer (S (1 .. Integer_Sized_String'Length));
> 
> Now that this is done, I have a question for you -- assuming that
> sizeof (int) on your machine is 4, what are you going to do with
> the additional six characters of s?
> 
> You may find it more useful to use the adasockets subprograms
> using Stream_Element_Array parameters instead of the
> string-oriented ones, because of their special treatment of CR and
> LF characters.
> 
> "Micha� Nowikowski" <godfryd@zamek.gda.pl> wrote in message
> news:20010611.085009.1789366143.627@zamek.gda.pl...
> > Hello
> >
> > Im writting a program which reads some data from a socket
> > (i used adasockets). And i've problem with converting
> > this date from String to fg Integer. How could i do that.
> > In C it woul look like:
> >
> >  char s[10];
> >  int i;
> >
> >  i = *(int*)s;
> >
> > but in Ada?
> >
> > thanks in advance
> > Godfryd
> 
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



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

* Re: converting - adasockets
  2001-06-11 11:45   ` Gerald Kasner
@ 2001-06-11 15:59     ` James Rogers
  2001-06-11 16:29       ` James Rogers
  0 siblings, 1 reply; 10+ messages in thread
From: James Rogers @ 2001-06-11 15:59 UTC (permalink / raw)


Gerald Kasner wrote:
> 
> "David C. Hoos, Sr." schrieb:
> >
> > Assuming the following Ada declarations,
> > S : String [1 .. 10];
> 
> This is not an Ada declaration ! (Pascal,Modula-2,Oberon-2...??)
> 
> > I : Integer;
> >
> Why unchecked conversion ?

Unchecked Conversion is necessary because the data is not a string
representation of an integer, but an integer value returned as
a string.  For instance, if the integer is represented as 32 bits,
the string will be returned as a four byte string. Those four bytes
need to be interpreted as an integer instead of as a string.

Ada provides two approaches for solving this problem.

The first is to use unchecked conversion as described by Mr.
Hoos. The second is to define a string and an integer to occupy
the same address. Simply copy the string from Adasockets into
the overlayed string, then read the value as an integer.

The C code example used by the original poster showed the C
equivalent of viewing a string as an integer. There was no conversion
of values as would occur using the 'value attribute.

Jim Rogers
Colorado Springs, Colorado USA



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

* Re: converting - adasockets
  2001-06-11 15:59     ` James Rogers
@ 2001-06-11 16:29       ` James Rogers
  2001-06-12  1:19         ` Ken Garlington
  0 siblings, 1 reply; 10+ messages in thread
From: James Rogers @ 2001-06-11 16:29 UTC (permalink / raw)


James Rogers wrote:
> 
> Gerald Kasner wrote:
> >
> > "David C. Hoos, Sr." schrieb:
> > >
> > > Assuming the following Ada declarations,
> > > S : String [1 .. 10];
> >
> > This is not an Ada declaration ! (Pascal,Modula-2,Oberon-2...??)
> >
> > > I : Integer;
> > >
> > Why unchecked conversion ?
> 
> Unchecked Conversion is necessary because the data is not a string
> representation of an integer, but an integer value returned as
> a string.  For instance, if the integer is represented as 32 bits,
> the string will be returned as a four byte string. Those four bytes
> need to be interpreted as an integer instead of as a string.
> 
> Ada provides two approaches for solving this problem.
> 
> The first is to use unchecked conversion as described by Mr.
> Hoos. The second is to define a string and an integer to occupy
> the same address. Simply copy the string from Adasockets into
> the overlayed string, then read the value as an integer.
> 
> The C code example used by the original poster showed the C
> equivalent of viewing a string as an integer. There was no conversion
> of values as would occur using the 'value attribute.

A simple example of defining an integer and a string to occupy the
same address 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
   String_Rep : String(1..4);
   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;
end Memory_Overlay;

Jim Rogers
Colorado Springs, Colorado USA



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

* Re: converting - adasockets
  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 16:33 ` tmoran
  2001-06-18 17:13 ` Stephen Leake
  3 siblings, 0 replies; 10+ messages in thread
From: tmoran @ 2001-06-11 16:33 UTC (permalink / raw)


>this date from String to fg Integer. How could i do that.
  I assume both your communicating systems use the same representation
for an integer.  If so, why send data as a string, why not send it
as an integer (or record containing whatever)?



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

* Re: converting - adasockets
  2001-06-11 16:29       ` James Rogers
@ 2001-06-12  1:19         ` Ken Garlington
  2001-06-12  4:41           ` James Rogers
  0 siblings, 1 reply; 10+ messages in thread
From: Ken Garlington @ 2001-06-12  1:19 UTC (permalink / raw)


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...


"James Rogers" <jimmaureenrogers@worldnet.att.net> wrote in message
news:3B24F26E.E198252C@worldnet.att.net...
: James Rogers wrote:
: >
: > Gerald Kasner wrote:
: > >
: > > "David C. Hoos, Sr." schrieb:
: > > >
: > > > Assuming the following Ada declarations,
: > > > S : String [1 .. 10];
: > >
: > > This is not an Ada declaration ! (Pascal,Modula-2,Oberon-2...??)
: > >
: > > > I : Integer;
: > > >
: > > Why unchecked conversion ?
: >
: > Unchecked Conversion is necessary because the data is not a string
: > representation of an integer, but an integer value returned as
: > a string.  For instance, if the integer is represented as 32 bits,
: > the string will be returned as a four byte string. Those four bytes
: > need to be interpreted as an integer instead of as a string.
: >
: > Ada provides two approaches for solving this problem.
: >
: > The first is to use unchecked conversion as described by Mr.
: > Hoos. The second is to define a string and an integer to occupy
: > the same address. Simply copy the string from Adasockets into
: > the overlayed string, then read the value as an integer.
: >
: > The C code example used by the original poster showed the C
: > equivalent of viewing a string as an integer. There was no conversion
: > of values as would occur using the 'value attribute.
:
: A simple example of defining an integer and a string to occupy the
: same address 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
:    String_Rep : String(1..4);
:    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;
: end Memory_Overlay;
:
: Jim Rogers
: Colorado Springs, Colorado USA





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

* Re: converting - adasockets
  2001-06-12  1:19         ` Ken Garlington
@ 2001-06-12  4:41           ` James Rogers
  0 siblings, 0 replies; 10+ messages in thread
From: James Rogers @ 2001-06-12  4:41 UTC (permalink / raw)


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



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

* Re: converting - adasockets
  2001-06-11  6:50 converting - adasockets 
                   ` (2 preceding siblings ...)
  2001-06-11 16:33 ` tmoran
@ 2001-06-18 17:13 ` Stephen Leake
  3 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2001-06-18 17:13 UTC (permalink / raw)


"Micha� Nowikowski" <godfryd@zamek.gda.pl> writes:

> Hello
> 
> Im writting a program which reads some data from a socket 
> (i used adasockets). And i've problem with converting
> this date from String to fg Integer. How could i do that.
> In C it woul look like:
> 
>  char s[10];
>  int i;
> 
>  i = *(int*)s;
> 
> but in Ada?

I have not seen AdaSockets, but I assume it returns data as a 'Stream',
not a 'String' (otherwise it's not Real Ada). So you want to use
Integer'read.

However, you should not be using type Integer, since the compiler does
not guarrantee it has the same size on both ends of the socket. You
should use your own integer type, with a specified size, or one of the
ones in package Interface.

-- 
-- Stephe



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

end of thread, other threads:[~2001-06-18 17:13 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2001-06-11 16:33 ` tmoran
2001-06-18 17:13 ` Stephen Leake

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