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=3.8 required=5.0 tests=BAYES_00,INVALID_MSGID, RATWARE_MS_HASH,RATWARE_OUTLOOK_NONAME autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c204c2633e999a33 X-Google-Attributes: gid103376,public From: "David C. Hoos, Sr." Subject: Re: Rep Clause Vaues?? Date: 1997/01/22 Message-ID: <01bc0862$77b689c0$018c71a5@dhoossr.iquest.com>#1/1 X-Deja-AN: 211497125 distribution: world references: <5c3oh5$7oh@uuneo.neosoft.com> content-type: text/plain; charset=ISO-8859-1 organization: Ada95 Press, Inc. mime-version: 1.0 newsgroups: comp.lang.ada Date: 1997-01-22T00:00:00+00:00 List-Id: Hi Robert, The main problem in your thinking is that 'Val and 'Pos only relate the enumeration VALUE and its POSITION in the declaration. See Ada95 LRM 13.4(11). The only way to obtain the representation is with unchecked conversion. The following code shows how it can be done. Note carefully the comments in the code. with Unchecked_Conversion; procedure Love is type mechs is (bolt,latch); for mechs use(bolt => 12, latch => 29); -- You should specify a size for this type here. -- E.g., for Mechs'Size use Integer'Size. -- In my experience, when specific representations are needed, e.g., to match -- some hardware or software interface, the size should be specified to insure -- portability, even if your current Ada compiler generates the correct size -- by default. mechanism : mechs := latch; -- Here we specify an integer tyoe which has exactly the same size as the -- mechs type. type Mechs_Bit_Pattern_Type is new Integer range 0 .. 2 ** Mechs'Size -1; for Mechs_Bit_Pattern_Type'Size use Mechs'Size; -- And, of, cource, we declare the object to be of our new type. bit_pattern : Mechs_Bit_Pattern_Type; -- here we declare two instances of Unchecked_Conversion to convert from the -- integer type to the enumerated type, and vice versa. function To_Mechs is new Unchecked_Conversion ( Source => Mechs_Bit_Pattern_Type, Target => Mechs ); function To_Mechs_Bit_Pattern_Type is new Unchecked_Conversion ( Source => Mechs, Target => Mechs_Bit_Pattern_Type ); begin --.... --.... -- And the assignment now becomes bit_pattern := To_Mechs_Bit_Pattern_Type (mechanism); --.... end Love; -- David C. Hoos, Sr., http://www.dbhwww.com http://www.ada95.com Robert B. Love wrote in article <5c3oh5$7oh@uuneo.neosoft.com>... > > Because of design aspects beyond my control I find myself wanting > to know how to return the bit pattern of an enumerated type that > has a specific rep spec. A simple example fragment is in order-- > > type mechs is (bolt,latch); > for mechs use(bolt => 12, latch => 29); > > mechanism : mechs := latch; > bit_pattern : integer; > > begin > .... > .... > bit_pattern := Integer(mechs'val(mechanism)); > .... > end > > What method can I use to get back the bit pattern or integer > value represented by the enumerated type? All my attempts at > type conversion and pos/val attributes have failed. > > All help appreciated. > ---------------------------------------------------------------- > Bob Love, rlove@neosoft.com (local) MIME & NeXT Mail OK > rlove@raptor.rmnug.org (permanent) PGP key available > ---------------------------------------------------------------- > >