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=1.3 required=5.0 tests=BAYES_00,INVALID_MSGID, MSGID_RANDY autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,937e32e44ab82465 X-Google-Attributes: gid103376,public From: Oliver Kellogg Subject: Re: Type conversion / computing problem Date: 2000/02/22 Message-ID: <88um2f$brk$1@nnrp1.deja.com>#1/1 X-Deja-AN: 588490124 References: <38AC2367.F60CA114@gmx.net> X-Http-Proxy: 1.0 sgt2-t4-1.atm-bb.de:3128 (Squid/2.1.PATCH1), 1.0 x39.deja.com:80 (Squid/1.1.22) for client 213.6.246.113, 62.104.220.70 Organization: Deja.com - Before you buy. X-Article-Creation-Date: Tue Feb 22 18:55:14 2000 GMT X-MyDeja-Info: XMYDJUIDokellogg Newsgroups: comp.lang.ada X-Http-User-Agent: Mozilla/4.6 [de]C-A084 (Win95; I) Date: 2000-02-22T00:00:00+00:00 List-Id: Bob Duff pointed the way; here's a possible implementation (I happen to have had a similar problem a while ago.) with Ada.Text_IO; with Interfaces, System, Unchecked_Conversion; use Interfaces; procedure RegTest is subtype Byte is Unsigned_8; subtype Var_Positive is Positive range 1 .. 8; type Byte_Array is array (Var_Positive range <>) of Byte; pragma Pack (Byte_Array); subtype Two_Bytes is Byte_Array (1 .. 2); subtype Four_Bytes is Byte_Array (1 .. 4); subtype RegType is Byte_Array (1 .. 8); function To_Bytes is new Unchecked_Conversion (Unsigned_64, RegType); function Maybe_Swap (Bytes : Byte_Array) return Byte_Array is Tmp : Byte_Array (1 .. Bytes'Length); begin if System."=" (System.Default_Bit_Order, System.Low_Order_First) then for I in Tmp'Range loop Tmp (I) := Bytes (Bytes'Last - I + 1); end loop; else Tmp := Bytes; end if; return Tmp; end Maybe_Swap; function Int2Reg (Value : Integer_32) return RegType is function To_Unsigned is new Unchecked_Conversion (Integer_64, Unsigned_64); U64 : Unsigned_64; begin U64 := To_Unsigned (Integer_64 (Value)); return Maybe_Swap (To_Bytes (U64)); end Int2Reg; function Reg2Int (Value : RegType) return Integer_32 is function To_Integer is new Unchecked_Conversion (Four_Bytes, Integer_32); begin return To_Integer (Maybe_Swap (Value (5 .. 8))); -- Interestingly, we can use fixed indexes here (5..8) -- regardless of host endianness. -- That is because on a little endian machine not only are -- the bytes in reverse sequence, but also the numbering of -- bytes is reversed. ("two wrongs make one right") end Reg2Int; package Byte_IO is new Ada.Text_IO.Modular_IO (Byte); RegVar : RegType; IntVar : Integer_32; begin RegVar := (1 .. 7 => 0, 8 => 1); -- one IntVar := Reg2Int (RegVar) + 1; -- two RegVar := Int2Reg (IntVar); for I in 1 .. 8 loop -- output should be (0,0,0,0,0,0,0,2) Ada.Text_IO.Put (' '); Byte_IO.Put (RegVar (I), Base => 16); end loop; Ada.Text_IO.New_Line; RegVar := (1 .. 8 => 16#FF#); -- minus one IntVar := Reg2Int (RegVar) - 1; -- minus two RegVar := Int2Reg (IntVar); for I in 1 .. 8 loop -- output should be (FF,FF,FF,FF,FF,FF,FF,FE) Ada.Text_IO.Put (' '); Byte_IO.Put (RegVar (I), Base => 16); end loop; Ada.Text_IO.New_Line; end RegTest; Sent via Deja.com http://www.deja.com/ Before you buy.