Reputation: 1
I am new to asn1 ,my agenda is i want to convert python dictionary into .asn format. when i ran the below code i got the following error
ParseError: Invalid ASN.1 syntax at line 1, column 1: '>!<"': Expected modulereference.
from __future__ import print_function
from binascii import hexlify
import asn1tools
specification=""""
Foo DEFINITIONS ::= BEGIN
Question ::= SEQUENCE {
id INTEGER,
question IA5String
}
Answer ::= SEQUENCE {
id INTEGER,
answer BOOLEAN
}
END
""""
Foo = asn1tools.compile_string(specification, 'uper')
Question = {'id': 2, 'question': u'Hi how r u?!'}
Answer ={'id': 2, 'answer': u'Hi i am good'}
encoded = Foo.encode('Question', Question)
encoded1 = Foo.encode('Answer', Answer)
decoded = Foo.decode('Question', Question)
print('Question:', Question)
print('Encoded:', hexlify(encoded).decode('ascii'))
print('Decoded:', decoded)
Upvotes: 0
Views: 2627
Reputation: 385274
Python string literals are not surrounded by four quotation marks, but three.
You can see that this is wrong, from the syntax highlighting in your question.
My Python installation rejects your code entirely. When I fix the closing delimiter to be three quotation marks (but leave the opening delimiter as it is) I get the issue you reported. (Please post your code verbatim next time.)
When I fix both, I get a new error:
asn1tools.codecs.EncodeError: answer: Expected data of type bool, but got Hi i am good.
This is because you're trying to use a string of English like a boolean; it should be:
Answer ={'id': 2, 'answer': True}
Finally, the decoding fails because you pass the wrong argument to Foo.decode
; it should be:
decoded = Foo.decode('Question', encoded)
Now it works.
from __future__ import print_function
from binascii import hexlify
import asn1tools
specification="""
Foo DEFINITIONS ::= BEGIN
Question ::= SEQUENCE {
id INTEGER,
question IA5String
}
Answer ::= SEQUENCE {
id INTEGER,
answer BOOLEAN
}
END
"""
Foo = asn1tools.compile_string(specification, 'uper')
Question = {'id': 2, 'question': u'Hi how r u?!'}
Answer ={'id': 2, 'answer': True}
encoded = Foo.encode('Question', Question)
encoded1 = Foo.encode('Answer', Answer)
decoded = Foo.decode('Question', encoded)
print('Question:', Question)
print('Encoded:', hexlify(encoded).decode('ascii'))
print('Decoded:', decoded)
Upvotes: 1