fileinsert
fileinsert

Reputation: 430

Python converting string to UTF

I have an OctetString value 0xe4c722d60a61 (it's a MAC address, actually) that my code is nerfing to UTF on conversion to a string e.g. 'äÇ"Ö\na'.

The code processes a lot of data and only a small subset is of this type.

How can I stop this from happening?

This is the output:

(Pdb) snmpVal
[ObjectType(ObjectIdentity(<ObjectName value object at 0x7f77d152eef0 tagSet <TagSet object at 0x7f77d620ba20 tags 0:0:6> payload [1.3.6.1.2.1.2.2.1.6.3]>), <PhysAddress value object at 0x7f77d152e7b8 encoding iso-8859-1 subtypeSpec <ConstraintsIntersection object at 0x7f77d61d7518 consts <ValueSizeConstraint object at 0x7f77d61d74e0 consts 0, 65535>> tagSet <TagSet object at 0x7f77d620b550 tags 0:0:4> payload [e4:c7:22:d6:0a:60]>)]
(Pdb) snmpVal[0][1]
<PhysAddress value object at 0x7f77d152e7b8 encoding iso-8859-1 subtypeSpec <ConstraintsIntersection object at 0x7f77d61d7518 consts <ValueSizeConstraint object at 0x7f77d61d74e0 consts 0, 65535>> tagSet <TagSet object at 0x7f77d620b550 tags 0:0:4> payload [e4:c7:22:d6:0a:60]>
(Pdb) str(snmpVal[0][1])
'äÇ"Ö\n`'
(Pdb) hex(snmpVal[0][1])
*** TypeError: 'PhysAddress' object cannot be interpreted as an integer

This is the code:

    cmdGen = cmdgen.CommandGenerator()
    errorIndication, errorStatus, errorIndex, snmpVal = cmdGen.getCmd(
        cmdgen.UsmUserData(user, 
                        authKey=authKey, 
                        privKey=privKey, 
                        authProtocol=authProto, 
                        privProtocol=privProto, 
                        securityEngineId=None
                ),
        cmdgen.UdpTransportTarget((sHost, 161)),
        '1.3.6.1.2.1.2.2.1.6.3' )

Upvotes: 0

Views: 1154

Answers (2)

Ilya Etingof
Ilya Etingof

Reputation: 5555

If you need to deal with raw OCTET STRING value, just call .asOctets() on the object. You will end up with bytes (Py3) or str (Py2). From here you could run unicode decoder if you need to turn OCTET STRING into unicode (which I doubt is a good idea).

If you want human-friendly representation of the MAC address, just call .prettyPrint() on the object. If it has been already resolved against a MIB, then pysnmp knows that it's a PhysAddress kind of OctetString so .prettyPrint() will turn it into colon-separated hex form.

Upvotes: 1

mypetlion
mypetlion

Reputation: 2524

You don't want to convert that to a UTF string by the sounds of your description. Python has a built-in function called hex that you can use. It converts any int value into a hex string. From there you can just add the colons yourself with some list comprehension and string joining. Try this:

a = hex(0xe4c722d60a61)
print(':'.join(a+b for a,b in zip(a[::2],a[1::2]))[3:])

Upvotes: 0

Related Questions