Home > Back-end >  Size end of discussion
Size end of discussion

Time:09-25

 
#include
#include


# define UINT32 unsigned int
# define UINT16 unsigned short int
# define UCHAR8 unsigned char

//define size end
Typedef enum _NetWorkDataEndianEnum
{
NET_BYTE_BIG_ENDIAN=0 x55,//big end
NET_BYTE_LITTLE_ENDIAN=0 xaa//small end
} NetByteOrder;

UINT32 GetLongFromBuffer (UCHAR8 * & amp; BufferPtr, NetByteOrder NetByteOrder)
{
It *//* define the value and init
UINT32 value=https://bbs.csdn.net/topics/0U;
/* */
If (bufferPtr!=NULL)
{
The switch (netByteOrder)
{
Case NET_BYTE_BIG_ENDIAN:
{
Value=https://bbs.csdn.net/topics/(* (bufferPtr + 0) <24) + (* (bufferPtr + 1) <16) + (* (bufferPtr + 2) <8) + (* (bufferPtr + 3) <0);
Printf ("====& gt; The value [0] : 02 x % \ n ", * (bufferPtr + 0));
Printf ("====& gt; The value [1] : 02 x % \ n ", * (bufferPtr + 1));
Printf ("====& gt; The value [2] : 02 x % \ n ", * (bufferPtr + 2));
Printf ("====& gt; The value [3] : 02 x % \ n ", * (bufferPtr + 3));

Printf ("===========& gt; 08 x value: 0 x % \ n ", value);

//bufferPtr=bufferPtr + 4;
}
break;
Case NET_BYTE_LITTLE_ENDIAN:
{
Value=https://bbs.csdn.net/topics/(UINT32) (((UINT32) (* (bufferPtr + 3)) <24) + (UINT32) ((UINT32) (bufferPtr + 2) (*) <16) + ((UINT32) (* (bufferPtr + 1)) <8) + ((UINT32) (* (bufferPtr + 0)) <0));
//bufferPtr=bufferPtr + 4;
}
break;
Default:
break;
}
}
The return value.
}

UINT16 GetBytesFromBuffer (UCHAR8 * ptrDst, UCHAR8 * & amp; PtrSrc, UINT16 bytesNum)
{
If (NULL==ptrDst | | ptrSrc=NULL https://bbs.csdn.net/topics/=
{
return 0;
}

for(int i=0; i<4. I++)
{
Printf ("====& gt; PtrSrc: 02 x % \ n ", * (ptrSrc + I));
}

Memcpy (ptrDst ptrSrc, bytesNum);

for(int i=0; i<4. I++)
{
Printf ("====& gt; PtrDst: 02 x % \ n ", * (ptrDst + I));
}

Return bytesNum;
}

Int main (int arg c, char * argv [])
{
QCoreApplication ab (arg c, argv);

UCHAR8 p [4];
for(int i=0; i<4. I++)
{
P [I]=I;//p: 0 x00010203
}
UCHAR8 * pTemp=p;

UINT32 _parameter1;//alarm parameters 1
_parameter1=GetLongFromBuffer (pTemp NET_BYTE_BIG_ENDIAN);

UINT32 _parameter2;//alarm parameters 2
GetBytesFromBuffer ((UCHAR8 *) & amp; _parameter2, pTemp, 4);

Printf ("===========& gt; 08 _para1:0 x % x \ n ", _parameter1);
Printf ("===========& gt; 08 _para2:0 x % x \ n ", _parameter2);

The return type ab. The exec ();
}


Results:


Don't quite understand, why the second result is different than the first one? Please welcome leaders teach, haha
  • Related