newbie sending hex values over UDP socket
Bill Seitz
fluxent at yahoo.com
Wed Sep 15 17:52:35 EDT 2004
Peter Hansen <peter at engcorp.com> wrote in message news:<qOGdnf1RP5VfyNrcRVn-oA at powergate.ca>...
> Bill Seitz wrote:
>
> > OK, so I'm clear on sending a string.
> >
> > If I want to send a numeric value, do I use something like
> > struct.pack('b',int)
> > ?
>
> I'd suggest playing a bit at the interactive prompt, watching
> the results of everything you do. For example, trying the
> above with a few values would tell you exactly what it does,
> provided you understand that you always see the *repr* value
> for what is output there, rather than the item itself. That
> is, when your expression results in a string (as the above
> does), you see the output of repr(s) instead of s itself,
> and thus non-printing characters are escaped and the output
> is surrounded by quotation marks:
>
> >>> import struct
> >>> struct.pack('b', 5)
> '\x05'
> >>> struct.pack('b', 255)
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> struct.error: byte format requires -128<=number<=127
> >>> struct.pack('b', -128)
> '\x80'
> >>> struct.pack('b', 127)
> '\x7f'
> >>> struct.pack('i', 127)
> '\x7f\x00\x00\x00'
> >>> struct.pack('h', 127)
> '\x7f\x00'
> >>> struct.pack('>h', 127)
> '\x00\x7f'
> >>> struct.pack('<h', 127)
> '\x7f\x00'
>
> And things like that... I think you'll be able to answer
> most or all such questions yourself after that.
>
> -Peter
Yeah, I've figured out a fair amount, thanks to y'all for getting me
going.
Current stump in this area: taking current time (time.time()) and
delivering as 4-byte blob.
>>> int(time.time())
1095284438
>>> t= int(time.time())
>>> hex(t)
'0x4148b710' ------ this looks promising, though the format smells
wrong
>>> format = 'l'
>>> struct.pack(format,t)
'\x10\xb7HA'
>>> format = 'L'
>>> struct.pack(format,t)
'\x10\xb7HA'
>>> format = 'q'
>>> struct.pack(format,t)
'\x10\xb7HA\x00\x00\x00\x00'
>>> format = 'i'
>>> struct.pack(format,t)
'\x10\xb7HA'
>>> format = 'I'
>>> struct.pack(format,t)
'\x10\xb7HA'
>>> format = 'P'
>>> struct.pack(format,t)
'\x10\xb7HA'
Can I just pass the blog as hex(t)? I suspect not.
More information about the Python-list
mailing list