comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Examining individual bytes of an integer
Date: Mon, 15 Oct 2018 00:04:50 +0300
Date: 2018-10-15T00:04:50+03:00	[thread overview]
Message-ID: <g2hpbjFpnrsU1@mid.individual.net> (raw)
In-Reply-To: <9d90fa3e-f800-4086-bf97-a65474a8140a@googlegroups.com>

On 18-10-14 22:15 , Henrik Härkönen wrote:
> I'd like to examine individual bytes of larger (in bit size)
> integer type. In C would probably move byte pointer etc. Also
> I'd need to have a way to construct the integer from individual bytes.
>
> Would this be "the Ada-way", the Unchecked_Conversion?

It depends... for this particular case, I would say "no", for the 
endianness reasons that Jeffrey described.

I would use the unsigned (modular) integer types in the predefined 
package Interfaces, such as Interfaces.Unsigned_32, which come with 
shift and rotate operations.

For example, to extract the most significant octet of a 32-bit unsigned 
integer with value 435343, I would do (assuming "use Interfaces"):

    Z : Unsigned_32 := 435343;

    MS_Octet : constant Unsigned_32 := Shift_Right (Z, 24);

To compose an unsigned 32-bit integer from the octets 77, 88, 99, and 
22, listed in big-endian order, I would do:

    Z : Unsigned_32 :=
          Shift_Left (77, 24)
       or Shift_Left (88, 16)
       or Shift_Left (99,  8)
       or             22;

(If you need lots of such expressions, you could define an "&" operator 
to make that look like 77 & 88 & 99 & 22.)

Using a record type with named octet components (and a Bit_Order clause) 
is ok, too, but then one needs some Unchecked_Conversions.

If you really want to inspect the structure of integers in memory, 
address by address, then I think the only way is the same as in C: make 
an octet pointer to the memory representation and read octet by octet. 
(But note that the Storage_Unit for some strange machines may not be 8 
bits.)

(I have become very sensitive to endianness problems because my daily 
work involves writing Ada programs targeted to big-endian SPARC 
machines, but tested initially on little-endian PCs.)

> ---8<---
>
> procedure Main is
>
>    type Byte is mod 256;
>    type Word is mod 2 ** 32;
>
>    type E2 is array(1 .. 4) of Byte;
>
>    Z: Word;
>    Y: E2;
>
>    function Convert_To_E2 is new Ada.Unchecked_Conversion (Source => Word,
>                                                            Target => E2);
>
>    function Convert_To_Word is new Ada.Unchecked_Conversion (Source => E2,
>                                                              Target => Word);
>
> begin
>    Y := Convert_To_E2(Word(435343));
>    Z := Convert_To_Word(Y);
> end Main;
>
> --->8---
>
> At least that looks nice and neat, I could also use record with
> assigned names to bytes if necessary.
>
> -Henrik
>

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

  parent reply	other threads:[~2018-10-14 21:04 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-14 19:15 Examining individual bytes of an integer Henrik Härkönen
2018-10-14 19:55 ` Jeffrey R. Carter
2018-10-14 21:28   ` Niklas Holsti
2018-10-15 10:18     ` AdaMagica
2018-10-15 18:43       ` Niklas Holsti
2018-10-15 20:18         ` Randy Brukardt
2018-10-16 10:18           ` AdaMagica
2018-10-16 11:55             ` Dmitry A. Kazakov
2018-10-16 22:35               ` Randy Brukardt
2018-10-17  8:10                 ` Dmitry A. Kazakov
2018-10-17  9:47                   ` briot.emmanuel
2018-10-17 10:04                     ` Dmitry A. Kazakov
2018-10-17 21:57                   ` Randy Brukardt
2018-10-16 22:33             ` Randy Brukardt
2018-10-17  6:51               ` Niklas Holsti
2018-10-17 22:01                 ` Randy Brukardt
2018-10-17  8:17               ` Dmitry A. Kazakov
2018-10-17 22:03                 ` Randy Brukardt
2018-10-17 22:10                 ` Randy Brukardt
2018-10-18  8:01                   ` Dmitry A. Kazakov
2018-10-18  9:33               ` AdaMagica
2018-10-18 21:18                 ` Randy Brukardt
2018-10-19  6:27                   ` Niklas Holsti
2018-10-19  8:28                     ` AdaMagica
2018-10-19 16:38                       ` Niklas Holsti
2018-10-14 21:04 ` Niklas Holsti [this message]
2018-10-15  7:49   ` Niklas Holsti
2018-10-15  8:55   ` Simon Wright
2018-10-15 10:52   ` Björn Lundin
2018-10-14 23:42 ` Matt Borchers
2018-10-14 23:45   ` Matt Borchers
2018-10-15  9:02     ` Simon Wright
2018-10-15  7:29   ` Dmitry A. Kazakov
2018-10-15  9:05   ` Simon Wright
2018-10-15 16:11     ` Simon Wright
2018-10-15 20:27   ` Randy Brukardt
2018-10-15  5:37 ` Henrik Härkönen
2018-10-15  7:42 ` Dmitry A. Kazakov
replies disabled

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