By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

declaring Hex in python

P: n/a
I want to use some old C code such as
#define GEN_STREAMTYPE_NULL 0x51
I need to send this to a server, but it must be declared as a unsigned
four byte constant. I can not just send a string. I love python, but
am looking for information where I can declare this and then send it
through a socket.
Also are there any classes or methods which can manipulate what is
returned, such as shifting bits returned.
Thank-you

Nov 3 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
PythonAni...@gmail.com wrote:
I want to use some old C code such as
#define GEN_STREAMTYPE_NULL 0x51
I need to send this to a server, but it must be declared as a unsigned
four byte constant. I can not just send a string. I love python, but
am looking for information where I can declare this and then send it
through a socket.
Also are there any classes or methods which can manipulate what is
returned, such as shifting bits returned.
Thank-you


See the "struct" module (http://docs.python.org/lib/module-struct.html)
for a couple of really useful functions. For example, you could use:
GEN_STREAMTYPE_NULL = 0x51
def send4bytes(num): .... mysocket.send(struct.pack("L", num))
.... send4bytes(GEN_STREAMTYPE_NULL)
As for bit manipulation--it's built in, just like in C.
hex(0x1234 & 0xFF) '0x34' hex(0x34 << 8)

'0x3400'

Hope this helps.

-- David

Nov 3 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.