I was trying to write signed integer value in a file in big endian.
I'm using this code :
int32_t swap_int32( int32_t val )
{
val = ((val << 8) & 0xFF00FF00) | ((val >> 8) & 0xFF00FF );
return (val << 16) | ((val >> 16) & 0xFFFF);
}
It works fine with negative value but I've some issues with positive value :
-5 gives me :
$> hexdump OUTPUT
ff ff ff fb
Which is correct
but 1337 gives me :
$> hexdump OUTPUT
00 00 05 39
Instead of (expected) 00 00 39 05.
Is there anyway to treat both cases and get correct result ? Thanks you.