newbie sending hex values over UDP socket

Peter Hansen peter at engcorp.com
Fri Sep 10 04:05:26 CEST 2004


Bill Seitz wrote:

> 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'

Very likely the latter one.  If not, then your "partner" is
unclear on the concept and needs to learn better how to
explain things like this. :-)

> 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

Hmm... the string you give above *is* the raw data... no
conversion with struct.pack or unpack is required.

> Sorry for the silly question... I'm trying not to look like a chump
> (to that partner)

Good strategy, asking here...  I could be wrong, but based
on what you say it really looks like doing sock.send('\x72\x69\x30')
(or perhaps sock.sendto()) is what you need.

Does the "ri0" representation look more like what you are
actually sending?  In other words, which is the more natural
representation of the data?  Whichever it is, it doesn't look
like you realized that *they are exactly the same thing*.  The
'\x72\x69\x30' form is merely an alternate way of telling the
Python compiler that you want it to create a string consisting
of three bytes, which in ASCII can be represented as "ri0".
The socket calls just take strings, which in Python are simply
sequences of bytes, nothing more...

-Peter



More information about the Python-list mailing list