newbie sending hex values over UDP socket

Jason Lai jmlai at uci.edu
Thu Sep 9 22:04:07 EDT 2004


Bill Seitz wrote:
> I'm working with an outside "partner" to whom I want to send messages
> (I want my app to send messages to his server/receiver). He requests
> that I use UDP and an "industry standard" for formatting the data
> itself.
> 
> That format is basically a bunch of hex bytes.
> 
> So instead of sending 3 alphanum chars
>   ri0
> 
> I have to send 3 bytes which he explains as being
>   72 69 30
> 
> Now, what the heck should I really be sending? I know he's giving just
> a simplified representation.
> 
> Am I really looking to send 
>   '726930'
> or 
>   '\x72\x69\x30'
> or
>   (something else)?
> 
> If I'm aiming for the 2nd format, how do I best generate it? If I'm
> going the other direction, it seems like
>   struct.unpack('sss','\x72\x69\x30')
> works OK
> 
> But going the other direction doesn't work:
>   struct.pack('sss','r','i','0') -> 'ri0'
> 
> If I'm going for the first format, then I guess
>   binascii.hexlify('ri0')
> is the answer, right?
> 
> Sorry for the silly question... I'm trying not to look like a chump
> (to that partner)

Well, since he says you're sending three bytes, I assume it's the second 
format. But the second format happens to be the same as the original.

Observe:

 >>> "\x72\x69\x30" == 'ri0'
True

Bytes are bytes, regardless of whether you write them as characters, 
binary, decimals, or hexidecimals.

  - Jason Lai



More information about the Python-list mailing list