Sign in to follow this  
chadmv

C++ to Python code

Recommended Posts

chadmv    645
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!

Share this post


Link to post
Share on other sites
chadmv    645
well the real code is:

float tx = (float)(xmesh->Tangents[i].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.

Share this post


Link to post
Share on other sites
samuraicrow    325
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=1044957488L
f=float(d)
d=long(f)

Share this post


Link to post
Share on other sites
chadmv    645
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 = 1044957488
float d = 0.19608
float f = 0.19608
DWORD f = 1044957488


So in my exporter I would get the float, 0.19608 and I need to output 1044957488

Share this post


Link to post
Share on other sites
samuraicrow    325
In that case try this:

class addressof:
def __init__(self, value):
self.address=value

print 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]

Share this post


Link to post
Share on other sites
Vorpy    869
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.

Share this post


Link to post
Share on other sites
mikeman    2942
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 object
ptr_float=pointer(tx)#Get a float* pointer
ptr_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.

Share this post


Link to post
Share on other sites
chadmv    645
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!

Share this post


Link to post
Share on other sites
Vorpy    869
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.

Share this post


Link to post
Share on other sites
Can you share a higher-level view of what you're trying to do? Why do you need to convert 0.19608 to 1044957488?

As mikeman mentioned, you can't simply write a 1:1 conversion from C to Python, and you don't want to. Whatever algorithm you're using that needs this conversion may be better suited to a more Pythonic method.

- Mike

Share this post


Link to post
Share on other sites
chadmv    645
I'm exporting the directx x file format. Specifically the DeclData template. This is where I output all the binormals and tangents of the mesh. These values are in floats but the specification wants DWORDS.

There's an old post on gamedev that sort of explains it:

Quote:
(the values are) compacted vertex buffer byte* casted to a DWORD* and then serialized out to disk.
It's wierd but it works...and there is no loss of percision.
unfortunately it's also not very human-readable.


Thanks again for the responses.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this