Convert byte array to float

Started by
3 comments, last by Washu 18 years, 11 months ago
Does anyone know how to convert a byte array into a float? I know how to convert it into an int, but converting from an int to a float is not working correctly so that approach is out. Any suggestions would be helpful, thanks in advance!
Advertisement
Can you post some code as to what exactly you are trying to do. Your description is a little vauge.


Do you have an array like this
unsigned char array[];

And what to put everything in it into a float array ?
float array2[];

Then its easy, just do

for(int i = 0; i < sizeOfArray; i++)
array2 = array;


That's it.
Author Freeworld3Dhttp://www.freeworld3d.org
Sorry for the confusion. I am coding in Java and dealing with sending information over the network through TCP/IP protocol.

I have a byte array so its something like byte[] buffer; This byte array holds the bytes of the float (4 bytes long since that is how long a float is). I need to convert this into a float, which is something like float f; Is that clear?
Well in C++, it would simply be something like this

unsigned char byteArray[size];
float *floatArray = &byteArray[0];



Or if you have a byte array allocated with the "new" keyword

unsigned char *byteArray = new unsigned char[size];
float *floatArray = (float *)byteArray;

Author Freeworld3Dhttp://www.freeworld3d.org
Use java.nio.ByteBuffer

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement