octet string conversion

Discussion in 'Python' started by Matthew, Jul 28, 2003.

  1. Matthew

    Matthew Guest

    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?

    Thanks very much
    -matthew
     
    Matthew, Jul 28, 2003
    #1
    1. Advertising

  2. Matthew

    Terry Reedy Guest

    "Matthew" <> wrote in message
    news:...
    > 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?


    Is this what you want?
    >>> s=repr('\000\002\263\254\264\351').replace(r'\x', ':')
    >>> s

    "':00:02:b3:ac:b4:e9'" # double " single ' ... single ' double "
    >>> s[2:-1]

    '00:02:b3:ac:b4:e9'

    Terry J. Reedy
     
    Terry Reedy, Jul 28, 2003
    #2
    1. Advertising

  3. 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


    I presume these are the repr of the strings you get back. If so, this
    is simply in binary format. To convert, take the ord of each character
    and print the hexadecimal code:

    >>> S = '\010\000 \301F\021'
    >>> ':'.join(['%x' % ord(x) for x in S])

    '8:0:20:c1:46:11'

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ Take the slow train / To my destination
    \__/ Sandra St. Victor
     
    Erik Max Francis, Jul 28, 2003
    #3
  4. On 28 Jul 2003 13:14:50 -0700, (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,

    >>> data = r"""

    ... \000\002\263\254\264\351
    ... \010\000\301\021
    ... \010\000\300\303
    ... \000\002\263\254\264\241
    ... """
    >>> for line in data.splitlines():

    ... if not line: continue # skip the first \n in data above
    ... print 'line: %r' %line
    ... oct3list = line.split('\\')[1:]
    ... print 'oct3list: %r'%oct3list
    ... octvals = [int(oct3,8) for oct3 in oct3list]
    ... print 'octvals: %r'%octvals
    ... hexstrings = ['%02X'%octval for octval in octvals]
    ... print 'hexstrings: %r'%hexstrings
    ... print 'joined with colons => %r' % ':'.join(hexstrings)
    ... print
    ...
    line: '\\000\\002\\263\\254\\264\\351'
    oct3list: ['000', '002', '263', '254', '264', '351']
    octvals: [0, 2, 179, 172, 180, 233]
    hexstrings: ['00', '02', 'B3', 'AC', 'B4', 'E9']
    joined with colons => '00:02:B3:AC:B4:E9'

    line: '\\010\\000\\301\\021'
    oct3list: ['010', '000', '301', '021']
    octvals: [8, 0, 193, 17]
    hexstrings: ['08', '00', 'C1', '11']
    joined with colons => '08:00:C1:11'

    line: '\\010\\000\\300\\303'
    oct3list: ['010', '000', '300', '303']
    octvals: [8, 0, 192, 195]
    hexstrings: ['08', '00', 'C0', 'C3']
    joined with colons => '08:00:C0:C3'

    line: '\\000\\002\\263\\254\\264\\241'
    oct3list: ['000', '002', '263', '254', '264', '241']
    octvals: [0, 2, 179, 172, 180, 161]
    hexstrings: ['00', '02', 'B3', 'AC', 'B4', 'A1']
    joined with colons => '00:02:B3:AC:B4:A1'

    and I got it wrong because I used fixed 2-char hex in upper case,
    and I'm showing the repr() of the line, which doubles the \'s,
    then you can fix it ;-)

    And get this for the data:

    line: \000\002\263\254\264\351
    joined with colons => '0:2:b3:ac:b4:e9'

    line: \010\000\301\021
    joined with colons => '8:0:c1:11'

    line: \010\000\300\303
    joined with colons => '8:0:c0:c3'

    line: \000\002\263\254\264\241
    joined with colons => '0:2:b3:ac:b4:a1'

    In future, please post actual data and corresponding output
    (or if you did this time, explain the anomalies). Otherwise
    it's a guessing game, and wastes all of our time except for
    silly finger exercises.

    Regards,
    Bengt Richter
     
    Bengt Richter, Jul 29, 2003
    #4
  5. Bengt Richter wrote:

    >On 28 Jul 2003 13:14:50 -0700, (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/
     
    Mike C. Fletcher, Jul 29, 2003
    #5
  6. Matthew

    Matthew Guest

    Thats great! I really appreciate the help. The F and spaces and ] etc,
    are not typos they real output via pysnmp.I used Erik Max Francis'
    code and it did exactly what I needed. Thank you all for
    contributing.

    -matthew
     
    Matthew, Jul 29, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lay
    Replies:
    2
    Views:
    656
    Steven Cheng[MSFT]
    Apr 8, 2004
  2. CVerma
    Replies:
    2
    Views:
    10,327
    C Verma
    Sep 28, 2004
  3. palmis

    octet string

    palmis, Dec 20, 2005, in forum: Java
    Replies:
    4
    Views:
    7,834
    palmis
    Dec 21, 2005
  4. Replies:
    7
    Views:
    1,238
    CBFalconer
    Nov 17, 2006
  5. miltizem

    Octet String Conversion

    miltizem, Jul 22, 2008, in forum: Java
    Replies:
    0
    Views:
    717
    miltizem
    Jul 22, 2008
Loading...

Share This Page