P1sec / pycrate

A Python library to ease the development of encoders and decoders for various protocols and file formats; contains ASN.1 and CSN.1 compilers.
GNU Lesser General Public License v2.1
380 stars 130 forks source link

how to replace asn1 enumerated name to raw value #221

Closed zywj closed 1 year ago

zywj commented 1 year ago

The result is like this below:

{
    "key": "enum_name"
}

But, I want the result is like this:

{
    "key": "23"
}
p1-bmu commented 1 year ago

Sorry, but I don't understand what you want to do. Where would the "23" value come from ?

zywj commented 1 year ago

Sorry, I didn't describe it clearly.

Test ::= SEQUENCE{
  userType UserType OPTIONAL,
}

UserType ::= ENUMERATED{
  admin (0),
  user (1)
}

I want to get the result like this:

{
  userType: 1    
}

But now, the result is this:

{
  userType: user 
}
p1-bmu commented 1 year ago

In ASN.1, ENUMERATED values are represented by their string, not their index. So, there is currently no alternative value representation for those abject apart the string one. On the other side, the ENUM class has a _get_index() method which returns the index related to the value set. For example:

from pycrate_asn1dir.S1AP import *
cp = S1AP_IEs.CauseProtocol                                                                                                                                                                         
cp.set_val('semantic-error')
print(cp.get_val()) # 'semantic-error'
print(cp._get_index()) # 4
zywj commented 1 year ago

Thanks for your valuable help