Lai
Lai

Reputation: 482

How do I encode using ASN.1?

I am very new to ANS1 encoding and I need some guidance.

...assuming I have the following ANS1

MyType ::= SEQUENCE 
{ 
--lets assume hex value here is "01-01" for myText1
myText1 [0] IMPLICIT OCTET STRING OPTIONAL, 
myCheck [1] IMPLICIT INTEGER {doNow(0), doLater(1)} DEFAULT doNow,
myText2 [2] OCTET STRING
}

If the value of myText2 is "12-34", will the following hex string 30-08-01-01-01-01-04-02-12-34 be considered as a correctly encoded data?

...if the optional myText1 is absent will, the encoding be

30-06-01-01-04-02-12-34

or

30-07-00-01-01-04-02-12-34?

Upvotes: 1

Views: 629

Answers (1)

Henry
Henry

Reputation: 43728

Lets say we want to encode the value {myText1 '0101'H, myCheck 0, myText2 '1234'H} with BER encoding rules:

30 0D                 SEQUENCE (universal tag 16, constructed) of length 13
   80 02 01 01        context specific implicit tag 0, length 2, value 0101
   81 01 00           context specific implicit tag 1, length 1, value 00
   A2 04              context specific explicit tag 2, length 4
      04 02 12 34     universal tag 4, length 2, value 1234

If an optional field is left out, the corresponding encoding is not present, for example if we leave out myText1 (note that myText2 is not optional):

30 09                 SEQUENCE of length 9
   81 01 00           context specific implicit tag 1, length 1, value 00
   A2 04              context specific explicit tag 2, length 4
      04 02 12 34     universal tag 4, length 2, value 1234

Note the usage of the constructed form bit when encoding the sequence value and the explicitly tagged myText2 value. Note further the difference between implicit and explicit tagging.

Upvotes: 3

Related Questions