PhixFlow Help

ASN.1 Tag Encoding

To find the correct settings for the tagType and berConstruct attributes of a Tag Node (with type set to BERTag), first convert the tag value into binary. The top 2 bits of the binary result represent the tag type - the value to set for the attribute tagType:

tagType

Bit 1

Bit 2

Universal

0

0

Application

0

1

Context

1

0

Private

1

1

The next (3rd) bit determines whether this tag represents a simple or constructed value, and therefore the value to set for the attribute berConstruct:

berConstruct

Bit 3

T

1

F

0

The remaining 5 bits represent the actual tag value. For example, if the given tag value is A5, in binary this is 10100101, and so:

  • tagType = Context (from bits 1and 2: 10)
  • berConstruct = T (from bit 3: 1)
  • value = 5 in decimal (from bits 4-8: 00101)

So the grammar for this Tag Node is:

<Tag name="tagValue" type="BERTag" discriminator="5" tagType="Context" berConstruct="T"/>


If the tag is specified as more than one byte, e.g. BF3B or 48955, then the actual tag value is greater than or equal to 31 (5 bits are not enough to encode values greater than 31; and the binary value for 31 is reserved, as described below). The remaining 5 bits on the first byte are all set to 1 indicating that the actual tag value is stored in the following bytes. To determine the actual tag value:

  • take the lowest 7 bits of the next byte
  • if the top bit of the next byte is 0 then the 7 bits represent the whole tag value
  • if the top bit is 1, take the lowest 7 bits of the 3rd byte, append them to the 7 bits from the 2nd byte, and check the top bit of the third byte
  • if the top bit of the third byte is 0 then you have the whole tag value; otherwise, move onto the next byte, and so on until the full tag value is found (when you hit a byte with the top bit set to 0).

For example, a tag specified as BF3B in hex or 48955 in decimal converts to binary as:

10111111 00111011


The top three bits indicate that the tagType is Context and berConstruct is T. The next 5 bits indicate that the tag value is greater than or equal to 31, and is encoded in subsequent bytes. The top bit of the next byte is 0, which indicates that this byte contains the whole tag value. The remaining 7 bits evaluate to the decimal value 59. The grammar for this tag is:

<Tag name="tagValue" type="BERTag" discriminator="59" tagType="Context" berConstruct="T"/>

Other pages

Please let us know if we could improve this page feedback@phixflow.com