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:
>>\010\000 \301F\021
>>\010\000 \300\303[
>>what I need though is hex strings like this:
>>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' )
 >>> hexify ( '\000\002\263\254\264\351' )


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list