newbie sending hex values over UDP socket

Dan Sommers me at privacy.net
Thu Sep 9 22:06:32 EDT 2004


On 9 Sep 2004 18:44:16 -0700,
fluxent at yahoo.com (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.

Looks like your partner's "industry standard" is plain old ASCII:

    >>> [ hex( ord( x )) for x in 'ri0' ]
    ['0x72', '0x69', '0x30']

> or 
>   '\x72\x69\x30'

Yeah, that's what I'd guess.  Did you type that byte string into an
interactive interpreter?

    >>> '\x72\x69\x30'
    'ri0'

> 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

If your data is in a byte string, then you're all set; just stuff the
byte string in question down the socket.  (I'm assuming that (a) there's
more to the protocol, or (b) the other end knows how much data to accept
based on the actual data received).

Regards,
Dan

-- 
Dan Sommers
<http://www.tombstonezero.net/dan/>
Never play leapfrog with a unicorn.



More information about the Python-list mailing list