Original Post
Hello. I am trying to figure out how to handle endian swaps with float data. I have a program where I read .lwo files into a buffer of BYTES (which is just a typedef's unsigned char). The .lwo files are big-endian, and so whenever I need to grab a piece of data, I need to perform an endian swap before I can use it. I have one for ints that uses bit shifts and works just fine. So, to grab a piece of int data, I'd do something like this: int iData = *(int*)&buffer[10]; endian_swap(iData); Where 10 is the location of the first byte in the datum. But since the endian_swap function uses bit shifts, I can't pass a float to it. So, what I've been doing is just casting the float to an int reference, like this: float fData = *(float*)&buffer[10]; endian_swap((int&)fData); I was thinking about devising a better solution. I thought I'd create some functions, like GetInt() and GetFloat, where I pass the address of the first byte in the buffer to the function, and it returns the data of the right type, with the right byte order. So I started to make a float function that looks something like this:
float GetFloat(BYTE *input)
{
return (float) (input[0] | (input[1] << 8) | (input[2] << 16) | (input[3] << 24));
}
Shouldn't this work? I tested this out with some real data. The value that it is SUPPOSED to return is -0.5f. Instead, it returns 191.0f. What gives? I thought that maybe I had inadvertantly set this up so that it would return the little-endian value, so I changed which bytes were shifted such that input[0] was shifted by 24 and input[3] was not shifted at all, but for some reason it yields the same exact value of 191.0f. I suspect I have completely the wrong idea.