From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Bj=c3=b6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Re: Examining individual bytes of an integer Date: Mon, 15 Oct 2018 12:52:27 +0200 Organization: A noiseless patient Spider Message-ID: References: <9d90fa3e-f800-4086-bf97-a65474a8140a@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Date: Mon, 15 Oct 2018 10:59:03 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="4e36004645db1849ebd145e879909ded"; logging-data="5259"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/6mJe2TPRZ6CSw2GTNBdUY" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 Cancel-Lock: sha1:H6Yt0O2GhafyqdZtPbZAzPxwVhM= In-Reply-To: Content-Language: sv-FI Xref: reader02.eternal-september.org comp.lang.ada:54585 Date: 2018-10-15T12:52:27+02:00 List-Id: On 2018-10-14 23:04, Niklas Holsti wrote: > > (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.) I usually decide how to work with the bytes (little- or bigEndian) and implement something like this type Byte is range 0..255; for Byte'Size use 8; type Byte_Array is array (Positive range <>) of Byte; subtype Byte_Array_2 is Byte_Array(1..2); subtype Byte_Array_4 is Byte_Array(1..4); procedure Swap4 (Bytes: in out Byte_Array_4) is Result : Byte_Array_4; begin Result := (1 => Bytes(4), 2 => Bytes(3), 3 => Bytes(2), 4 => Bytes(1)); Bytes := Result; end Swap4; ------------------------------------------------- procedure Swap2 (Bytes: in out Byte_Array_2) is Result : Byte_Array_2; begin Result := (1 => Bytes(2), 2 => Bytes(1)); Bytes := Result; end Swap2; ------------------------------------------------- function Mirror (Data : Byte_Array) return Byte_Array is begin case Data'Length is when 2 => declare Tmp : Byte_Array_2 := Data; begin case System.Default_Bit_Order is when System.High_Order_First => null; --ppc when System. Low_Order_First => Swap2(Tmp); --x86 end case; return Tmp; end; when 4 => declare Tmp : Byte_Array_4 := Data; begin case System.Default_Bit_Order is when System.High_Order_First => null; --ppc when System. Low_Order_First => Swap4(Tmp); --x86 end case; return Tmp; end; when others => raise Constraint_Error with "not implemented lenght" & Data'Length'img; end case; end Mirror; ------------------------------------------------- Then I call Mirror once (at input), and have the bytes in the same order no matter endianess. The I can modify the bytes as I please. I also call Mirror again at output, so I can write the modifications in a machine friendly way. (code is taken from project, but altered here without compilation, especially the 4byte part) -- -- Björn