现在的位置: 首页 > 综合 > 正文

“big-endian”和“little-endian”的三种参考处理方式

2013年10月03日 ⁄ 综合 ⁄ 共 2287字 ⁄ 字号 评论关闭
    // =============================================
    // Class for 16 bit numbers.
    // =============================================
    template <class T>
    class CBe16
    {
        typedef union 
        {
            T i;
            struct 
            {
                char a,b;
            } c;
        } Union16;
    
        char a,b;
    
    public:
        // Sets "big-endian" value.
        void operator ()(T nValue)
        {
            Union16 u16;
            u16.i = nValue;
    
            a = u16.c.b;
            b = u16.c.a;
        }
    
        // Returns "little-endian" value.
        T operator ()()
        {
            Union16 u16;
    
            u16.c.b = a;
            u16.c.a = b;
    
            return u16.i;
        }
    };
    
    typedef CBe16<short> CBeShort;
    typedef CBe16<unsigned short> CBeUShort;
    
    // =============================================
    // Class for 32 bit numbers.
    // =============================================
    template <class T>
    class CBe32
    {
        typedef union 
        {
            T i;
            struct 
            {
                char a,b,c,d;
            } c;
        } Union32;
    
        char a,b,c,d;
    
    public:
        // Sets "big-endian" value.
        void operator ()(T nValue)
        {
            Union32 u32;
            u32.i = nValue;
    
            a = u32.c.d;
            b = u32.c.c;
            c = u32.c.b;
            d = u32.c.a;
        }
    
        // Returns "little-endian" value.
        T operator ()()
        {
            Union32 u32;
    
            u32.c.d = a;
            u32.c.c = b;
            u32.c.b = c;
            u32.c.a = d;
    
            return u32.i;
        }
    };
    
    typedef CBe32<int> CBeInt;
    typedef CBe32<unsigned int> CBeUInt;
    typedef CBe32<long> CBeLong;
    typedef CBe32<unsigned long> CBeULong;
    typedef CBe32<float> CBeFloat;
    
    // =============================================
    // Class for 64 bit numbers.
    // =============================================
    class CBeDouble
    {
        typedef union 
        {
            double d;
            struct 
            {
                char a,b,c,d,e,f,g,h;
            } c;
        } Union64;
    
        char a,b,c,d,e,f,g,h;
    
    public:
        // Sets "big-endian" value.
        void operator ()(double dValue)
        {
            Union64 u64;
            u64.d = dValue;
    
            a = u64.c.h;
            b = u64.c.g;
            c = u64.c.f;
            d = u64.c.e;
            e = u64.c.d;
            f = u64.c.c;
            g = u64.c.b;
            h = u64.c.a;
        }
    
        // Returns "little-endian" value.
        double operator ()()
        {
            Union64 u64;
    
            u64.c.h = a;
            u64.c.g = b;
            u64.c.f = c;
            u64.c.e = d;
            u64.c.d = e;
            u64.c.c = f;
            u64.c.b = g;
            u64.c.a = h;
    
            return u64.d;
        }
    };
    
    typedef unsigned short     uint16;
    typedef unsigned long      uint32;
    
    //-------------------------------------
    // Basic swaps
    //-------------------------------------
    template <typename T>
    inline T WordSwapC( T w )
    {
       uint16 temp;
    
       temp  = ((*((uint16 *)&w) & 0xff00) >> 8);
       temp |= ((*((uint16 *)&w) & 0x00ff) << 8);
    
       return *((T*)&temp);
    }
    
    template <typename T>
    inline T DWordSwapC( T dw )
    {
       uint32 temp;
    
       temp  =   *((uint32 *)&dw)               >> 24;
       temp |= ((*((uint32 *)&dw) & 0x00FF0000) >> 8);
       temp |= ((*((uint32 *)&dw) & 0x0000FF00) << 8);
       temp |= ((*((uint32 *)&dw) & 0x000000FF) << 24);
    
       return *((T*)&temp);
    }
    
    //-------------------------------------
    // Fast swaps
    //-------------------------------------
    template <typename T>
    inline T WordSwapAsm( T w )
    {
       __asm
       {
          mov ax, w
          xchg al, ah
       }
    }
    
    template <typename T>
    inline T DWordSwapAsm( T dw )
    {
       __asm
       {
          mov eax, dw
          bswap eax
       }
    }
    

抱歉!评论已关闭.