From: Gerhard Wolf on
Hi,

how can i revert the 4 DWords of a float value. In detail:
Regard a float as 4 DWords (regardless of sign...) i need a function
that reverts this float from:

DWord1 DWord2 DWord3 DWord4 to

DWord4 DWord3 DWord2 DWord1.

I don't know how to do this and if the are already available functions
in C++.
From: Gerhard Wolf on
Gerhard Wolf schrieb:
> Hi,
>
> how can i revert the 4 DWords of a float value. In detail:
> Regard a float as 4 DWords (regardless of sign...) i need a function
> that reverts this float from:
>
> DWord1 DWord2 DWord3 DWord4 to
>
> DWord4 DWord3 DWord2 DWord1.
>
> I don't know how to do this and if the are already available functions
> in C++.
Ups i was wrong:

i need to reverse the Bytes(!) of a 32Bit value from
Byte0 Byte1 Byte2 Byte3 to
Byte3 Byte2 Byte1 Byte1 (not DWord)
From: Jensen Somers on
Gerhard Wolf wrote:
> Gerhard Wolf schrieb:
>> Hi,
>>
>> how can i revert the 4 DWords of a float value. In detail:
>> Regard a float as 4 DWords (regardless of sign...) i need a function
>> that reverts this float from:
>>
>> DWord1 DWord2 DWord3 DWord4 to
>>
>> DWord4 DWord3 DWord2 DWord1.
>>
>> I don't know how to do this and if the are already available functions
>> in C++.
> Ups i was wrong:
>
> i need to reverse the Bytes(!) of a 32Bit value from
> Byte0 Byte1 Byte2 Byte3 to
> Byte3 Byte2 Byte1 Byte1 (not DWord)

This can be done very easily using bitwise operations. You will need to
take a mask of the input variable for each byte and bit shift that
result left into a new variable.

Start by taking a mask of Byte0 (0xFF000000) and store the result into a
new variable. Take a mask of Byte1 on the input variable (0x00FF0000)
and bit shift that left 8 bits into the result variable. 0x0000FF00 for
Byte2 and left shift it 16 bits, 0x000000FF for Byte3 and left shift it
24 bits.

- Jensen
From: Francis Glassborow on
Jensen Somers wrote:
> Gerhard Wolf wrote:
>> Gerhard Wolf schrieb:
>>> Hi,
>>>
>>> how can i revert the 4 DWords of a float value. In detail:
>>> Regard a float as 4 DWords (regardless of sign...) i need a function
>>> that reverts this float from:
>>>
>>> DWord1 DWord2 DWord3 DWord4 to
>>>
>>> DWord4 DWord3 DWord2 DWord1.
>>>
>>> I don't know how to do this and if the are already available
>>> functions in C++.
>> Ups i was wrong:
>>
>> i need to reverse the Bytes(!) of a 32Bit value from
>> Byte0 Byte1 Byte2 Byte3 to
>> Byte3 Byte2 Byte1 Byte1 (not DWord)
>
> This can be done very easily using bitwise operations. You will need to
> take a mask of the input variable for each byte and bit shift that
> result left into a new variable.
>
> Start by taking a mask of Byte0 (0xFF000000) and store the result into a
> new variable. Take a mask of Byte1 on the input variable (0x00FF0000)
> and bit shift that left 8 bits into the result variable. 0x0000FF00 for
> Byte2 and left shift it 16 bits, 0x000000FF for Byte3 and left shift it
> 24 bits.
>
That looks an awfully complicated way to do something that is
essentially simple. C and C++ guarantee that you can alias any type with
an array of unsigned char (bytes) so:

void reverse_bytes(float * f){
unsigned char * bytes = (unsigned char *) f;
char temp;
temp = bytes[0] , bytes[0] = bytes[3], bytes[3] = temp;
temp = bytes[1] , bytes[1] = bytes[2], bytes[1] = temp;
}

However this whole process is fraught with problems because you are
assuming that reversing the order of the bytes produces a valid float value.

I suspect that what you really want is to convert between a little
endian and a big endian representation. The question is then where and
how the original values are supplied.

From: Ralph D. Ungermann on
>> Gerhard Wolf wrote:
>>> i need to reverse the Bytes(!) of a 32Bit value from
>>> Byte0 Byte1 Byte2 Byte3 to
>>> Byte3 Byte2 Byte1 Byte1 (not DWord)


Francis Glassborow wrote:
> void reverse_bytes(float * f){
> unsigned char * bytes = (unsigned char *) f;

Despite of all my laziness, I'd write reinterpret_cast<...> here.

> char temp;
> temp = bytes[0] , bytes[0] = bytes[3], bytes[3] = temp;
> temp = bytes[1] , bytes[1] = bytes[2], bytes[1] = temp;

Are you really sure about the precedence of = vs. , ???
Semikolons would also do here. But why not

std::swap( bytes[0], bytes[3] );
std::swap( bytes[1], bytes[2] );


> However this whole process is fraught with problems because you are
> assuming that reversing the order of the bytes produces a valid float
> value.

Agreed! So let's wrap it:

class taolf // byte-reversed float
{
char bytes[sizeof(float)];

public:
taolf ( float const &f ) { *this = f; }

taolf & operator = ( float const & f )
{
for ( int i=0; i < sizeof(float); ++i )
bytes[i] = reinterpret_cast<unsigned char const*>(&f)
[ sizeof(float)-i-1 ];
return *this;
}

operator float() const
{
char r[sizeof(float)];
for ( int i=0; i < sizeof(float); ++i )
r[i] = bytes[sizeof(float)-i-1];
return *reinterpret_cast<float *>(r);
}

char* reversed_bytes() { return bytes; }
};


-- ralph