Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > reading hex values elegantly from a datagram

Reply
Thread Tools

reading hex values elegantly from a datagram

 
 
Dave
Guest
Posts: n/a
 
      08-26-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
 
Reply With Quote
 
 
 
 
George Young
Guest
Posts: n/a
 
      08-26-2003
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.

 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      08-26-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thoughts after reading the thread: "How to elegantly get the enum code" DaveB C++ 0 04-17-2010 08:44 AM
winsock2 & wxWidgets: application hangs after reading UDP datagram,then accessing wxTextCtrl Lars Uffmann C++ 7 02-21-2008 11:11 AM
What's the difference between Datagram.read and Datagram.receive? - Java 11 08-26-2005 08:01 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments