From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!aioe.org!W4+pUJJ+LMQSnRdpBvjvmw.user.46.165.242.91.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Sockets, Streams, and Element_Arrays: Much confusion Date: Sat, 31 Dec 2022 15:16:05 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <2c8f31a2-a39a-cb31-a764-28e0ac84ea1a@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: gioia.aioe.org; logging-data="40702"; posting-host="W4+pUJJ+LMQSnRdpBvjvmw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org"; User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 X-Notice: Filtered by postfilter v. 0.9.2 Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:64750 List-Id: On 2022-12-31 14:50, Mark Gardner wrote: > On 31/12/2022 15:11, Dmitry A. Kazakov wrote: >> On 2022-12-31 13:11, Mark Gardner wrote: >> >>> ... >> >> Stream_Element_Array is declared in Ada.Streams as >> >>     type Stream_Element_Array is >>        array(Stream_Element_Offset range <>) of >>           aliased Stream_Element; >> >> For communication purpose it is an array of octets. Your datagram is >> represented as a Stream_Element_Array or a slice of. > > According to RM 13.13.1, "Stream_Element is mod implementation-defined" > which to me says there is no guarantee that they will be octets, unless > this is specified elsewhere? GNAT.Sockets is GNAT-specific. All GNAT compilers have Stream_Element 8 bits. I can imagine some DSP implementation with Stream_Element of 32 bits. But realistically add pragma Assert (Stream_Element'Size >= 8); and be done with that. > So, how would I do this directly on the elements? I mean, if it is an > octet-array to a string, I expect an element-to-element copy, or type > conversion to work, but what about integers? Hmm, it cannot be string. It is a string encoded in some specific way (usually most peculiar (:-)). Then you will have to decode it into your machine type e.g. Wide_String or String. An UTF-8 string you could put into String ignoring Ada's Latin-1 stuff, as most people would do: function To_String (S : Stream_Element_Array) return String is begin return Result : String (1..S'Length) do for I in S'Range loop Result (Positive (I - S'First + 1) := Character'Val (S (I)); end loop; end return; end To_String; > Do I need to do something like > My_Int:=Unsigned_8(octet(1))+2**8*Unsigned_8(octet(2)); > or whatever endianness demands? Or is this the time to learn how to use > Unchecked_Conversion? There are many ways to convert Stream_Element_Array "in situ" to string. However, in network protocols you rarely have any strings at all. Usually it is some binary data you need to decode into some machine type. (So is String or Wide_String actually) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de