Notifications
Clear all

[Closed] FloatSwap

Hello everyone again…

I need to make this C function to maxscript:

float FloatSwap( float f ) {union {

float f;
unsigned char b[4];
} dat1, dat2;

dat1.f = f;
dat2.b[0] = dat1.b[3];
dat2.b[1] = dat1.b[2];
dat2.b[2] = dat1.b[1];
dat2.b[3] = dat1.b[0];

return dat2.f;

}

This function just split’s the float into 4 bytes and them swap them. My problem is that I don’t know how to split the float into 4 bytes, the union in c does that job but there’s no union on maxscript.

Any suggestions?

3 Replies

I’m not sure you can do that directly in MAXScript.

So one solution would be create a MAXScript extension with C++ using the SDK. That way you can re-use your C code.

I’ve created a project (Visual Studio .NET 2003) for you so you can see how it’s done. Right now it only implements the FloatSwap function but you can add whatever function you want. Look at the endian.cpp file to view the function code.

In the release folder there’s a file named endian.dlx. This is the actual MAXScript extension and should be placed in your MAX plug-ins folder.

Now, in MAX you could write something like this:

floatSwap 123.343

to convert the little endian float to big endian.

But seems like could be precission issues. For example, this:

floatSwap 3.1

returns 2.7186e+023. But this:

floatSwap 3.12

returns 0.0 and I don’t think it’s right…

Anyway, hope that helps.

Thank you very much Jonathan. That solution it’s perfect and much easier for me.
Now about the float precision, I don’t see any problem with that.
Do it yourself:

f=fopen “c:\ est.dat” “wb”
WriteFloat f (FloatSwap 3.1)

fclose f

It’s perfect.

Thank you, once again for the huge help.

Great!.

Yes, seems like the decimal precission for the number stored in memory is higher than the number shown in the maxscript listener.

Greets.