精彩代码:交换float的endian

使用C语言Union交换float的各字节(转换endian)。来自http://www.gamedev.net/page/resources/_/technical/game-programming/writing-endian-independent-code-in-c-r2091


float FloatSwap( float f )
{
  union
  {
    float f;
    unsigned char b[4];
  } dat1, dat2;

  dat1.f = f;
  dat2.b[0] = dat1.b[3];
  dat2.b[1] = dat1.b[2];
  dat2.b[2] = dat1.b[1];
  dat2.b[3] = dat1.b[0];
  return dat2.f;
}

本文地址:http://zisxks.com/2014/03/25/code-trick-swap-float-use-union
分享到 Share: 0

欢迎转载,转载请注明作者及出处zisxks.com,请勿用于商业用途