473,602 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

declaring Hex in python

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
1 4440
PythonAni...@gm ail.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(s truct.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
25451
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was wondering if there is an equivalent of #define in Python for declaring constants. Is there an elegant way of defining constants in Python? Please let me know. Thanks, Sriram
5
2394
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and relevant announcements. If you're interested, drop me a note. But if for some reason you think that this discussion is fine here at the c.l.py, please let me know. ** LONG POST AHEAD **
6
1635
by: Cactus | last post by:
Hi, If I got a list is it possible to declare a variable from the items in that list? Code Sample: Blob = i = 5 for listitems in Blob: i += 1 listitems = i
2
4638
by: lamthierry | last post by:
Calling a python method from C++ has the following signature: PyObject * PyObject_CallMethod(PyObject *self, char *method_name, char *arg_format, ...); I'm having trouble figuring out how to declare self. Let's say my python file is called stuff.py and is like the following, doMath() is defined in stuff.py and is not part of any class:
1
2860
by: Maarten Terlingen | last post by:
Sample: ClassA: ClassB ClassB: ClassC ClassA x = new Class C I want to know the declaring type of C. x.GetType() returns C x.GetType().BaseType returns B x.GetType().BaseType.BaseType returns A, but I don't know the depth in my
5
2223
by: param | last post by:
Declaring struct as static is creating problem with newer version of CC compiler 5.7 in solaris. e.g. static struct new_str { int a; int b; };
8
7502
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
8
3406
by: HMS Surprise | last post by:
I looked in the language but did not find a switch for requiring variables to be declared before use. Is such an option available? Thanks, jvh
9
1334
by: =?ISO-8859-1?Q?Janne_H=E4rk=F6nen?= | last post by:
Hello, Is there a simple way to resolve declaring class of a method at runtime ? Consider this simple example: $ python Python 2.5.1 (r251:54863, May 18 2007, 16:56:43) on cygwin Type "help", "copyright", "credits" or "license" for more information.
0
7920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.