[EMAIL PROTECTED] wrote:
> -------- Original-Nachricht --------
>> Datum: Thu, 17 Apr 2008 07:12:01 -0700
>> Von: Thomas Hruska <[EMAIL PROTECTED]>
>> Instead of swapping bytes, you could always use shifting. Bits in bytes
>> are almost always ordered the same way (bit 7 to 0).
>>
>> char MyInt[sizeof(int)];
>>
>> for (int x = 0; x < sizeof(int); x++)
>> {
>> MyInt[x] = (char)(YourInt & 0xFF);
>> YourInt = YourInt >> 8;
>> }
>>
>> --
>
> Thanks! But , that method does not work with floats, right?
> Heres what im currently using to swap floats (seems to be giving me the right
> numbers):
>
> float floatSwap(float f)
> {
> char* value = (char *)&f;
> char buffer[4];
> buffer[0] = value[3];
> buffer[1] = value[2];
> buffer[2] = value[1];
> buffer[3] = value[0];
> return *( (float *) &buffer );
> };
That won't work. 'buffer' goes away at the end of the function call, so
the result is undefined behavior. (i.e. you have a bug in your code
that depends on the stack not being modified when you go to read it later).
floats and doubles are tricky to begin with.
--
Thomas Hruska
CubicleSoft President
Ph: 517-803-4197
*NEW* MyTaskFocus 1.1
Get on task. Stay on task.
http://www.CubicleSoft.com/MyTaskFocus/