473,799 Members | 2,822 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4103
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(M AX_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
1250
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 sent before some are read, they are silently dropped. (By "silently," I mean, "tcpdump doesn't record these as dropped packets.") This is a problem because while my consumer can handle the overall load easily, the requests often come in large...
4
33774
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
2533
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 am really desperate now, because I havent' been able to locate the origin of the problem for a couple of days now.. PROBLEM: the message digest obtained differs each time I execute the code, but works perfectly when applying the "control", that...
6
6278
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 at 255, but I need it to be larger. Iain
4
2791
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 | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE,strAddress); Now when I send, I' ve error in runtime at this line:
14
3275
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 make things simpler and better readable I'd make all default parameters named parameters so that you can decide for yourself which one to pass and which not, rather than relying on massively overlaoded methods which hopefully provide the best...
2
1417
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? I'm just concerned that I may be getting multiple messages in a single datagram. I'm using a connectionless multicast paradigm where there are multiple receivers listening asynchronously, and a single sender sending synchronously.
1
3587
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 Main(string args) {
0
1031
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 UdpClient(); byte sendDatagram = { 0xff, 0xff, 0xff, 0xff, 0x54, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x20, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x20, 0x51, 0x75, 0x65, 0x72, 0x79, 0x0 };
0
9689
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10495
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4148
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.