Trouble converting hex to decimal?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Earl Eiland

    #1

    Trouble converting hex to decimal?

    I'm trying to process the IP packet length field, as recorded by pcap
    (Ethereal) and recovered using pcapy. When I slice out those bytes, I
    get a value that shows in '\x00' format, rather than '0x00'. Neither
    int() nor eval() are working. How do I handle this?

    Earl Eiland

  • Steve Holden

    #2
    Re: Trouble converting hex to decimal?

    Earl Eiland wrote:
    [color=blue]
    > I'm trying to process the IP packet length field, as recorded by pcap
    > (Ethereal) and recovered using pcapy. When I slice out those bytes, I
    > get a value that shows in '\x00' format, rather than '0x00'. Neither
    > int() nor eval() are working. How do I handle this?
    >
    > Earl Eiland
    >[/color]

    You could either reconstruct the value yourself from the ord() values of
    the individual bytes, or look at the struct module which can help you
    with this kind of decoding task.

    regards
    Steve
    --
    Meet the Python developers and your c.l.py favorites March 23-25
    Come to PyCon DC 2005 http://www.pycon.org/
    Steve Holden http://www.holdenweb.com/

    Comment

    • Alan McIntyre

      #3
      Re: Trouble converting hex to decimal?

      Earl,

      Try this:
      [color=blue][color=green][color=darkred]
      >>> ord('\x00')[/color][/color][/color]
      0

      or:
      [color=blue][color=green][color=darkred]
      >>> import struct
      >>> struct.unpack(' b', '\x00')[/color][/color][/color]
      (0,)

      If you're needing to pull values out of multiple bytes (shorts, longs,
      floats, etc.), have a look at the struct module. Here's an example:
      [color=blue][color=green][color=darkred]
      >>> struct.unpack(' f', '\x00\x00(B')[/color][/color][/color]
      (42.0,)

      Hope this helps,
      Alan

      Earl Eiland wrote:[color=blue]
      > I'm trying to process the IP packet length field, as recorded by pcap
      > (Ethereal) and recovered using pcapy. When I slice out those bytes, I
      > get a value that shows in '\x00' format, rather than '0x00'. Neither
      > int() nor eval() are working. How do I handle this?
      >
      > Earl Eiland
      >[/color]

      Comment

      • Jorgen Grahn

        #4
        Re: Trouble converting hex to decimal?

        On Sat, 05 Feb 2005 06:51:32 -0700, Earl Eiland <eee@nmt.edu> wrote:[color=blue]
        > I'm trying to process the IP packet length field, as recorded by pcap
        > (Ethereal) and recovered using pcapy. When I slice out those bytes, I
        > get a value that shows in '\x00' format, rather than '0x00'. Neither
        > int() nor eval() are working. How do I handle this?[/color]

        From my unpublished protocol analyzing hack:

        class Ip:
        "IPv4 header"
        def __init__(self, frame):
        (vi, tos, tlen,
        id, ffoff,
        ttl, proto, checksum,
        source,
        dest) = struct.unpack(' ! BBH HH BBH LL', frame[:20])
        self.len = 4 * (vi & 0xf)
        if proto==6:
        self.proto=Tcp
        elif proto==17:
        self.proto=Udp
        elif proto==1:
        self.proto=Icmp
        self.source = Address(source)
        self.dest = Address(dest)

        That doesn't take IP options into account (or are they part of Ip.len? I
        forget.), but it has the nifty feature that IP.proto tells the caller what
        factory function (if any) she should feed the rest of the frame into.

        /Jorgen

        --
        // Jorgen Grahn <jgrahn@ Ph'nglui mglw'nafh Cthulhu
        \X/ algonet.se> R'lyeh wgah'nagl fhtagn!

        Comment

        Working...