reading hex values elegantly from a datagram

Dave davbucko at yahoo.com
Tue Aug 26 08:29:46 EDT 2003


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




More information about the Python-list mailing list