ASN.1 CHOICE

The ASN.1 CHOICE type is used when you need to define a type whose value can be one of several different types, depending on which of those types is needed at a particular time. The alternatives in a CHOICE type's list of choices are similar to the elements in a SEQUENCE: identifiers are followed by a type name.

Example

In the following example, the type MessageType is defined as a choice of either an IA5String called text or an INTEGER called codedNumeric. Since IA5String and INTEGER have different tags, the receiver of a value of this type can tell which choice has been made.

MessageType ::= CHOICE {
                  text         OCTET STRING,
                  codedNumeric INTEGER}

In the next example, type Division is defined as a choice of manufacturing, r-and-d, or unassigned. The alternatives include two SEQUENCEs. Because the first two alternatives are both SEQUENCEs, the standard type's tags are replaced with user-defined context-specific tags.

Division ::= CHOICE {
                  manufacturing  [0] IMPLICIT SEQUENCE {
                                         plantID      INTEGER,
                                         majorProduct IA5String},
                  r-and-d        [1] IMPLICIT   SEQUENCE {
                                         labID          INTEGER,
                                         currentProject IA5String},
                 unassigned      [2] IMPLICIT  NULL
                 }
CHOICE encoding example
Division ::= CHOICE {
         manufacturing      SEQUENCE {
                   plantID        INTEGER,
                   majorProduct   OCTET STRING},
                   
         r-and-d            SEQUENCE {
                   labID          INTEGER,
                   currentProject OCTET STRING}
}

currentAssignment Division ::=
         r-and-d : { labID 48,
                    currentProject '44582D37'H}

In BER, the SEQUENCE defined for r-and-d in the example above is encoded as follows:

A1 09
     80 01 30
     81 04 44 58 2D 37

Constraints

The CHOICE type can be constrained by a single value, by type inclusion, and by constraints on its alternatives (inner subtyping).

Related Topics