/*****************************************************************/
//name : convert.h
//function : 底层 socket 流字节序列转换类定义
//copyright :
//author : markhuang
//date : 2007-08-21
/**************************************************************/
#if !defined(AFX_CCONVERT_H__INCLUDED_)
#define AFX_CCONVERT_H__INCLUDED_
class CConvert
{
public:
static unsigned long long int ReadUInt64(char *pBuffer);
static unsigned long long int ReadUInt64_r(char *pBuffer);
//读速度优化后版本
static void WriteUInt64(char *pBuffer ,unsigned long long int value);
static unsigned int ReadUInt32(const char *pBuffer);
//读速度优化后版本
static unsigned int ReadUInt32_r(char *pBuffer);
static void WriteUInt32(char *pBuffer ,unsigned int value);
static void WriteUInt16(char * pBuffer,unsigned short value);
static unsigned short ReadUInt16(const char *pBuffer);
//读速度优化后版本
unsigned short CConvert::ReadUInt16_r(char *pBuffer);
static void WriteUInt8(char * pBuffer,unsigned char value);
static unsigned char ReadUInt8(const char *pBuffer);
static unsigned int ReadByteArray(const char *pBuffer, unsigned int Len);
static unsigned int WriteByteArray(char *pBuffer,unsigned int Len);
};
#endif
/*****************************************************************/
//name : convert.cpp
//function : 底层 socket 流字节序列转换类实现
//copyright : tencent
//author : markhuang
//date : 2007-08-21
/**************************************************************/
#include "convert.h"
#include <string.h>
void CConvert::WriteUInt16(char * pBuffer,unsigned short value)
{
pBuffer[0] = (unsigned char)((value >> 8) & 0xFF);
pBuffer[1] = (unsigned char)((value) & 0xFF);
pBuffer += 2;
}
unsigned short CConvert::ReadUInt16(const char *pBuffer)
{
unsigned short ch1, ch2, ret = 0;
ch1 = (unsigned short)(pBuffer[0] & 0xff);
ch2 = (unsigned short)(pBuffer[1] & 0xff);
ret = ((ch1 << 8) + (ch2 << 0));
return ret;
}
unsigned short CConvert::ReadUInt16_r(char *pBuffer)
{
char ch1 = 0;
char buff[2] = {0};
memcpy(buff,pBuffer,2);
ch1 = buff[1];
buff[1] = buff[0];
buff[0] = ch1;
return *((unsigned short *)buff);
}
void CConvert::WriteUInt32(char *pBuffer ,unsigned int value)
{
pBuffer[0] = (unsigned char)((value >> 24) & 0xFF);
pBuffer[1] = (unsigned char)((value >> 16) & 0xFF);
pBuffer[2] = (unsigned char)((value >> 8) & 0xFF);
pBuffer[3] = (unsigned char)((value >> 0) & 0xFF);
}
unsigned int CConvert::ReadUInt32(const char *pBuffer)
{
unsigned int ch1, ch2, ch3, ch4, ret=0;
ch1 = (unsigned int)(pBuffer[0] & 0xff);
ch2 = (unsigned int)(pBuffer[1] & 0xff);
ch3 = (unsigned int)(pBuffer[2] & 0xff);
ch4 = (unsigned int)(pBuffer[3] & 0xff);
ret = ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
return ret;
}
unsigned int CConvert::ReadUInt32_r(char *pBuffer)
{
char ch1 = 0;
char buff[4] = {0};
memcpy(buff,pBuffer,4);
ch1 = buff[3];
buff[3] = buff[0];
buff[0] = ch1;
ch1 = buff[2];
buff[2] = buff[1];
buff[1] = ch1;
return *((unsigned int *)buff);
}
void CConvert::WriteUInt64(char *pBuffer ,unsigned long long int value)
{
pBuffer[0] = (unsigned char)((value >> 56) & 0xFF);
pBuffer[1] = (unsigned char)((value >> 48) & 0xFF);
pBuffer[2] = (unsigned char)((value >> 40) & 0xFF);
pBuffer[3] = (unsigned char)((value >> 32) & 0xFF);
pBuffer[4] = (unsigned char)((value >> 24) & 0xFF);
pBuffer[5] = (unsigned char)((value >> 16) & 0xFF);
pBuffer[6] = (unsigned char)((value >> 8) & 0xFF);
pBuffer[7] = (unsigned char)((value >> 0) & 0xFF);
}
unsigned long long int CConvert::ReadUInt64(char *pBuffer)
{
unsigned long long int ch1, ch2, ch3, ch4, ch5,ch6,ch7,ch8;
ch1 = (unsigned int)(pBuffer[0] & 0xff);
ch2 = (unsigned int)(pBuffer[1] & 0xff);
ch3 = (unsigned int)(pBuffer[2] & 0xff);
ch4 = (unsigned int)(pBuffer[3] & 0xff);
ch5 = (unsigned int)(pBuffer[4] & 0xff);
ch6 = (unsigned int)(pBuffer[5] & 0xff);
ch7 = (unsigned int)(pBuffer[6] & 0xff);
ch8 = (unsigned int)(pBuffer[7] & 0xff);
unsigned long long int ret= 0;
ret = ((ch1 << 56) + (ch2 << 48) + (ch3 << 40) + (ch4 << 32)
+(ch5 << 24) + (ch6 << 16) + (ch7 << 8) + (ch8 << 0));
return ret;
}
unsigned long long int CConvert::ReadUInt64_r(char *pBuffer)
{
char ch1 = 0;
char buff[8] = {0};
memcpy(buff,pBuffer,8);
//for(int i = 0;i < 8;++i)
//{
//buff[i] = pBuffer[i];
//}
ch1 = buff[7];
buff[7] = buff[0];
buff[0] = ch1;
ch1 = buff[6];
buff[6] = buff[1];
buff[1] = ch1;
ch1 = buff[5];
buff[5] = buff[2];
buff[2] = ch1;
ch1 = buff[4];
buff[4] = buff[3];
buff[3] = ch1;
return *((unsigned long long int *)buff);
}
void CConvert::WriteUInt8(char * pBuffer,unsigned char value)
{
pBuffer[0] = (unsigned char)(value);
}
unsigned char CConvert::ReadUInt8(const char *pBuffer)
{
return (unsigned char)pBuffer[0];
}
unsigned int CConvert::ReadByteArray(const char *pBuffer, unsigned int uiLen)
{
//assert(0);
return 0;
}
unsigned int CConvert::WriteByteArray(char *pBuffer, unsigned int Len)
{
//assert(0);
return 0;
}