reading hex values elegantly from a datagram

Discussion in 'Python' started by Dave, Aug 26, 2003.

  1. Dave

    Dave Guest

    Hi,

    This is doubtless a really dumb question but is there an elegant way
    of reading numbers formatted as hex from a datgram?

    I get a datgram which contains values like this:

    ---DATAGRAM---
    2aef2d43etc...
    ---/DATAGRAM--
    where this represents 4 numbers: 2a, ef, and 2d43.

    At the moment I am doing crazy things like converting it to a string,
    slicing characters off (ie id = packet[0:2]), adding 0x to the front
    and exec-ing the whole thing to get an integer.

    I'll put my code in if you really want but it's just embarassing.

    This is my first time trying to do something with sockets, so please
    tell me if I have entirely missed the point. I know that you can use
    read to get a number of bytes, but will that solve my problem? Can
    anyone point me in the direction of a fool (ie me) proof tutorial on
    using bits in python because I can't find anything. For example, is
    it possible to read the binary data? At least then I am sure about
    the packet structure (for a RADIUS RFC 2865 server, in the unlikely
    event that anyone is interested).

    TIA

    Dave
     
    Dave, Aug 26, 2003
    #1
    1. Advertising

  2. Dave

    George Young Guest

    On Tue, 26 Aug 2003 05:29:46 -0700, Dave wrote:

    > This is doubtless a really dumb question but is there an elegant way
    > of reading numbers formatted as hex from a datgram?
    >
    > I get a datgram which contains values like this:
    >
    > ---DATAGRAM---
    > 2aef2d43etc...
    > ---/DATAGRAM--
    > where this represents 4 numbers: 2a, ef, and 2d43.
    >
    > At the moment I am doing crazy things like converting it to a string,
    > slicing characters off (ie id = packet[0:2]), adding 0x to the front
    > and exec-ing the whole thing to get an integer.
    >
    > I'll put my code in if you really want but it's just embarassing.
    >
    > This is my first time trying to do something with sockets, so please
    > tell me if I have entirely missed the point. I know that you can use
    > read to get a number of bytes, but will that solve my problem? Can
    > anyone point me in the direction of a fool (ie me) proof tutorial on
    > using bits in python because I can't find anything. For example, is
    > it possible to read the binary data? At least then I am sure about
    > the packet structure (for a RADIUS RFC 2865 server, in the unlikely
    > event that anyone is interested).


    First order improvement: instead of adding 0x and exec'ing,
    use the built in "int" function: int('2a', 16) ==> 42 .

    Better, use the "struct" standard library module, something like:
    import struct
    data = mysocket.recv(MAX_UDP_LEN)
    struct.unpack('BBH', data[0:4]) ==> (42, 239, 17197)

    Of course you must look carefully at byte order, signed/unsigned,
    etc. Do:
    import struct
    help(struct)
    for details.
     
    George Young, Aug 26, 2003
    #2
    1. Advertising

  3. Dave

    Paul Rubin Guest

    (Dave) writes:
    > At the moment I am doing crazy things like converting it to a string,
    > slicing characters off (ie id = packet[0:2]), adding 0x to the front
    > and exec-ing the whole thing to get an integer.


    Don't ever exec data from an untrusted source. Use int(packet[0:2], 16).
     
    Paul Rubin, Aug 26, 2003
    #3
    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. -
    Replies:
    11
    Views:
    1,742
    Gordon Beaton
    Aug 26, 2005
  2. Replies:
    10
    Views:
    6,345
    Neredbojias
    Aug 19, 2005
  3. Bengt Richter
    Replies:
    6
    Views:
    504
    Juha Autero
    Aug 19, 2003
  4. Lars Uffmann
    Replies:
    7
    Views:
    794
    Lars Uffmann
    Feb 21, 2008
  5. DaveB
    Replies:
    0
    Views:
    288
    DaveB
    Apr 17, 2010
Loading...

Share This Page