Three reference processing methods of "BIG-Endian" and "Little-endian"

xiaoxiao2021-03-06  76

/ / ==============================================

// Class for 16 bit numbers.

/ / ==============================================

Template

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 Cbesh;

Typedef CBE16 CBEUSHORT;

/ / ==============================================

// Class for 32 bit numbers.

/ / ==============================================

Template

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 cbeint;

TypeDef CBE32 cbeuint;

Typedef CBE32 cbelong;

Typedef CBE32 Cbeulong;

TypedEf CBE32 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

Inline T Wordswapc (T W) {

UINT16 TEMP;

Temp = ((((((((((((((uint16 *) & w) & 0xff00) >> 8);

Temp | = ((* ((uint16 *) & w) & 0x00FF) << 8);

Return * ((t *) & temp);

}

Template

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

Inline T Wordswapasm (T W)

{

__ASM

{

Mov AX, W

Xchg Al, AH

}

}

Template

Inline T DWORDSWAPASM (T DW)

{

__ASM

{

MOV EAX, DW

Bswap EAX

}

}

转载请注明原文地址:https://www.9cbs.com/read-120581.html

New Post(0)