473,324 Members | 2,248 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

reading hex values elegantly from a datagram

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
Jul 18 '05 #1
2 4060
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.

Jul 18 '05 #2
da******@yahoo.com (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).
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jonathan Ellis | last post by:
I seem to be running into a limit of 64 queued datagrams. This isn't a data buffer size; varying the size of the datagram makes no difference in the observed queue size. If more datagrams are...
4
by: Gregory Hassett | last post by:
Hello, Does anyone know how to get the maximum size of a datagram for a UDP Socket created via .NET's System.Net.Sockets.Socket class? Thanks!
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
6
by: Iain King | last post by:
Hi. I've been looking everywhere for this and can't find it, apologies if I'm being obtuse: How do I set the max datagram packet size? I'm using the socket module. It seem like it's hardcoded...
4
by: alberto.castellin | last post by:
Hi to all, I've this problem: I create a datagram socket. I create datagram socket with function 'Create'. The line of my code when I create is: m_pSocket->Create(port,SOCK_DGRAM,FD_READ |...
14
by: cody | last post by:
I got a similar idea a couple of months ago, but now this one will require no change to the clr, is relatively easy to implement and would be a great addition to C# 3.0 :) so here we go.. To...
2
by: DaTurk | last post by:
I know in TCP if you send a message down the wire, the OS may lump subsequent messages into a buffer before sending it across the wire, for efficiency. But, will the OS do the same thing with UDP?...
1
by: greenxiar | last post by:
My code is below and the platform is Win2K3 R2: using System; using System.Net; using System.Net.Sockets; using System.Threading; namespace TechUDPBroadcast { class Program { static void...
0
by: LeAdGuEsT | last post by:
On form load it preforms this action: string IP = "*"; int Port = *; IPEndPoint RemoteEndPoint = new IPEndPoint(System.Net.IPAddress.Parse(IP), Port); UdpClient Client = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.