其他语言

本类阅读TOP10

·基于Solaris 开发环境的整体构思
·使用AutoMake轻松生成Makefile
·BCB数据库图像保存技术
·GNU中的Makefile
·射频芯片nRF401天线设计的分析
·iframe 的自适应高度
·BCB之Socket通信
·软件企业如何实施CMM
·入门系列--OpenGL最简单的入门
·WIN95中日志钩子(JournalRecord Hook)的使用

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
“big-endian”和“little-endian”的三种参考处理方式

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

    // =============================================
    // 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
       }
    }
    



相关文章

相关软件