octet string conversion

Mike C. Fletcher mcfletch at rogers.com
Tue Jul 29 07:27:25 CEST 2003


Bengt Richter wrote:

>On 28 Jul 2003 13:14:50 -0700, ruach at chpc.utah.edu (Matthew) wrote:
>
>  
>
>>I am working on some software that uses SNMP to get information from
>>routers and switches. I am using the pysnmp module (v2.0.8) to do the
>>snmp part. The problem that I am having is that when I query a router
>>for mac addresses pysnmp returns octetstrings like this:
>>
>>\000\002\263\254\264\351
>>\010\000 \301F\021
>>\010\000 \300\303[
>>\000\002\263\254\264\241
>>
>>what I need though is hex strings like this:
>>
>>0:e0:7d:de:5:48
>>0:e0:7d:c8:dc:9f
>>8:0:36:4:3b:de
>>0:80:ad:3a:9e:2b
>>
>>Can anyone tell me how to convert the octet strings to hex strings?
>>    
>>
>Assuming that your data is really octal character representations like \nnn,
>and the spaces, F, and [ in the middle lines are typos, and that your need example
>has nothing to do with the example data,
>
(I'll go from the other assumption, that he has simple strings of 
octets, that is, binary data)

 >>> def hexify( octets ):
...     return ":".join( [ '%x'%(ord(c)) for c in octets ] )
...
 >>> hexify ( '\010\000 \301F\021' )
'8:0:20:c1:46:11'
 >>> hexify ( '\000\002\263\254\264\351' )
'0:2:b3:ac:b4:e9'

HTH,
Mike

_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list