difference between "enum" and "choice" type in ASN1

811 Views Asked by At

can somebody please explain when an "enum" variable is used vs when a "choice" variable is used in ASN1 ?

2

There are 2 best solutions below

0
On

An ENUMERATED type in ASN.1 is a used when you have a list of named items you would like to choose from such as

Colors ::= ENUMERATED {red, green, blue, yellow, purple}

A CHOICE type is used when you would like to choose between different ASN.1 types.

PreferredContactMethod ::= CHOICE {
  mail        PrintableString,
  mobilePhone NumericString,
  email       VisibleString,
  homephone   NumericString
}

Only one item in the choice can be selected at a time.

0
On

CHOICE is richer because the alternatives can be whatever type you want.

PreferredContactMethod ::= CHOICE {
  mail        PrintableString,
  identity    SEQUENCE { 
     firstName PrintableString,
     lastName PrintableString
  }
}

However, you are right. You could use a CHOICE to define a enumeration (you could also use named INTEGERs) ...

Colors ::= ENUMERATED {red, green, blue, yellow, purple}

Colors ::= INTEGER {red(0), green(1), blue(2), yellow(3), purple(4)}

Colors ::= CHOICE {red NULL, green NULL, blue NULL, yellow NULL, purple NULL}

I have seen the 3 ways used in specifications (note that the encoding will be different)

My 2 cents: if your type is clearly an enumeration, use ENUMERATED