comp.lang.ada
 help / color / mirror / Atom feed
From: "news.broadpark.no" <etjensen@broadpark.no>
Subject: Re: Type convertion
Date: Tue, 11 Mar 2008 21:56:02 +0100
Date: 2008-03-11T21:56:02+01:00	[thread overview]
Message-ID: <47d6f184@news.broadpark.no> (raw)
In-Reply-To: <47d6c231$1_1@glkas0286.greenlnk.net>

"Stuart" <stuart@0.0> wrote in message 
news:47d6c231$1_1@glkas0286.greenlnk.net...
> "news.broadpark.no" <etjensen@broadpark.no> wrote in message 
> news:47d6ae9b$1@news.broadpark.no...
>>I have a simple question. I'm working on an Ada package I have received.
>>
>> - Is it possible to covert a Boolean to Integer in an easy way
>> - Is it possible to covert an enum to Integer in an easy way
>
> Yes - but it depends on what integers you want.
>
> First, try checking up on the 'pos attribute for enumerated types - ARM 
> 3.5.5
>
>> In the second question i typically have something like this:
>>
>>   type My_Type is  (AB, CD, EF, GH, IJ);
>>
>> Now, I have a declaration:
>>
>>   MyTest : My_Type;
>>
>> The result is stored in MyTest. I'm writing an interface to a C based 
>> program and I need to convert it to Integer. I'm new to Ada, so any hint 
>> would help.
>
> If you are interfacing to a C program you perhaps should look at ARM Annex 
> B.  For the interface you should use one of the Integer_n/Unsigned_n 
> types.
>
> If you simply want the mapping
>    AB => 0
>    CD => 1
>    EF => 2
>    GH => 3
>    IJ => 4
>
> you could use
>    My_Type'pos(MyTest)
>
> Another way is to declare an array of Interfaces.Integer_n  indexed by 
> My_Type which contains the values you want for each value of My_Type.
>
> You could use representation clauses for My_Type and 
> Unchecked_Conversions - but this is probably not a good idea.

My_Type'pos and My_Type'val did the job. Thank's!

Eirik




      reply	other threads:[~2008-03-11 20:56 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-03-11 16:10 Type convertion news.broadpark.no
2008-03-11 16:59 ` Ed Falis
2008-03-11 20:45   ` Ludovic Brenta
2008-03-12 20:36     ` Simon Wright
2008-03-12 20:45       ` Ludovic Brenta
2008-03-13  8:33         ` Maciej Sobczak
2008-03-24  2:14           ` David Thompson
2008-03-11 17:39 ` Jeffrey R. Carter
2008-03-11 20:53   ` news.broadpark.no
2008-03-11 21:40     ` Ludovic Brenta
2008-03-11 17:49 ` Stuart
2008-03-11 20:56   ` news.broadpark.no [this message]
replies disabled

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