C++ to Python code

Started by
11 comments, last by chadmv 17 years, 1 month ago
Hello, How would I go about converting this C++ code to Python:
DWORD d = 1044957488;
float * fd = (float*)(&d);
float f = *fd;
DWORD * df = (DWORD*)(&f);
I'm converting a Maya x exporter to Python and I'm not sure how to get pointers and addresses of basic data types in Python. Thanks!
Advertisement
It's been a while since I've used C++, but isn't that code basically just creating a pointer to DWORD d? Can you post any more of the code you're trying to convert?
well the real code is:

float tx = (float)(xmesh->Tangents.x);DWORD * dtx = (DWORD*)(&tx);os << *dtx << ",\n";


Basically I need to cast the address of the float to a DWORD pointer and then output the value as a dword.
I know that Python has a long integer type that is arbitrarily large. It's been a while since I learned Python, though, and I know that Python 2.2.x didn't support a DWORD type.

I do know that Python's references act like pointers without any of the special syntactic sugar that C++ needs.

Try something like this to simulate the first code you posted:
d=1044957488Lf=float(d)d=long(f)
That isn't quite what I'm looking for. I don't need to just convert the float to a long, I need python to interpret the bytes of the float as if they were the bytes of a long. For example, in the first code I posted

DWORD d = 1044957488;float * fd = (float*)(&d);float f = *fd;DWORD * df = (DWORD*)(&f);cout << "DWORD d = " << d << endl;cout << "float d = " << *fd << endl;cout << "float f = " << f << endl;cout << "DWORD f = " << *df << endl;


The output is

DWORD d = 1044957488float d = 0.19608float f = 0.19608DWORD f = 1044957488


So in my exporter I would get the float, 0.19608 and I need to output 1044957488
In that case try this:
class addressof:  def __init__(self, value):    self.address=valueprint addressof(d)


-edit-
No, that creates a new reference to a reference. What you want is something that extends the float class that doesn't have a defined __str__ method.

-edit2-
Well, at least it will give you the address of addressof.address. :-(

[Edited by - samuraicrow on March 2, 2007 9:26:02 PM]
I think you'll want to use python's struct module. It provides functions for packing and unpacking python data to and from strings containing binary data.

In python you should never ever try to think in terms of addresses of data because python just isn't designed that way.
You really can't translate C programs to Python on a 1-1 basis. What you want to translate is the algorithm, not the individual C commands. So, you have to think long and hard about doing such things in Python. However, there is a way:the ctypes module, which is standard since Python 2.5. If you have <2.5, you can download ctypes. The script would be like:

from ctypes import *tx=c_float(42.42)#Create a C-float objectptr_float=pointer(tx)#Get a float* pointerptr_dword=cast(ptr_float,POINTER(c_uint))#Convert to dword* print ptr_dword[0]#Dereference the dword* pointer and print the value


However, this is like writing a C program using the Python interpreter. So if you're going to do the translation this way, I don't really see the point.
I'm writing a maya x file exporter for maya 8.5 and some of the data needs to be output that way. Unfortunately the Maya Python bindings are only version 2.4. Thanks all for the help!
The struct module has been around for a while and it can be used to do what you want. Using it to convert from a float to an int is kind of strange, but bit level reinterpretation is an unusual operation.

This topic is closed to new replies.

Advertisement