0

I have some pyasn1 octect string objects defined like this:

LInfo.componentType = namedtype.NamedTypes(namedtype.NamedType('XXX', univ.OctetString().subtype(subtypeSpec=constraint.ValueSizeConstraint(2, 2)).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)))

When I decode the asn.1 using pyasn.1 library, it translates the value to its ascii representation, for example I get "&/" but I need to show the numeric value, in hex representation. For example in this case instead of &/ I need 262F. Obviously I can extract &/ or whatever I find in there and convert it manually, like:

value.asOctects().encode("HEX")

but then I can't write it back in the field in this format.

Is there an easy way to manipulate the object before printing it out in such a way that I can see 262F in the final pretty print without modifying the asn.1 definition (that I can't change as it is given to me)?

4

1 回答 1

0

你可以做的是继承OctetString类,覆盖它的prettyPrint方法,然后用解码器注册你的新类。你的prettyPrint回报是最终印刷出来的。

from pyasn1.codec.ber import decoder
from pyasn1.type import univ


class OctetString(univ.OctetString):
    def prettyPrint(self):
        return self._value


class OctetStringDecoder(decoder.OctetStringDecoder):
    protoComponent = OctetString('')


decoder.tagMap[OctetString.tagSet] = OctetStringDecoder()
decoder.typeMap[OctetString.typeId] = OctetStringDecoder()

这是原始代码

于 2018-11-15T05:32:55.023 回答