What is the quickest way to reverse the endianness of a 16 bit and 32 bit integer. I usually do something like (this coding was done in Visual Studio in C++):
union bytes4
{
__int32 value;
char ch[4];
};
union bytes2
{
__int16 value;
char ch[2];
};
__int16 changeEndianness16(__int16 val)
{
bytes2 temp;
temp.value=val;
char x= temp.ch[0];
temp.ch[0]=temp.ch[1];
temp.ch[1]=x;
return temp.value;
}
__int32 changeEndianness32(__int32 val)
{
bytes4 temp;
temp.value=val;
char x;
x= temp.ch[0];
temp.ch[0]=temp.ch[1];
temp.ch[1]=x;
x= temp.ch[2];
temp.ch[2]=temp.ch[3];
temp.ch[3]=x;
return temp.value;
}
Is there any faster way to do the same, in which I don't have to do so many calculations?