Jump to content
  • Advertisement
Sign in to follow this  
devronious

Bytes to float...

This topic is 4697 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
langauge?

in c and many of its offspring itll look like


// one 4 byte array
char bytearray[4];
float f = *((float*)bytearray);

// many floats
char byteArray[4 * numFloats];

float * floatArray = (float*)byteArray;

float f = floatArray[ index ];



EDIT: fixed horrible error!

Share this post


Link to post
Share on other sites
Quote:
Original post by RDragon1
better question: why do you want to?
I assume it's either for serialization purposes or to perform bithacks on the integer representation of a float.
Either way it's fairly common thing to do..

Share this post


Link to post
Share on other sites
Quote:
Original post by devronious
trying to convert a dang mesh vertexbuffer into vector3's
Any particular reason you can't just cast a pointer directly instead?
At least dealing with the structures at the byte level shouldn't be necessary as long as you're storing regular 32-bit floats in both.

Share this post


Link to post
Share on other sites
the Mesh.LockVertexBuffer() method returns a byte array. For what I have figured out so far, I have to use the vertexDeclaration of the mesh to walk thru the byte data to determine what's what.

Any easier ideas are most certainly appreciated. But still it would be nice to know how to convert from four bytes to a float.

I'm using c# and I tried:

byte[] ba = {100, 1, 114, 123};
float f = (float)ba;

and it says can't convert.

-Devin

Share this post


Link to post
Share on other sites
Quote:
Original post by devronious
Any easier ideas are most certainly appreciated. But still it would be nice to know how to convert from four bytes to a float.

I'm using c# and I tried:

byte[] ba = {100, 1, 114, 123};
float f = (float)ba;

and it says can't convert.

-Devin


This may not be the most efficient way, but it sure is easy.

byte[] ba = {250,62,246,66};
float f = System.BitConverter.ToSingle(ba,0); // f now equals 123.123F

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!