基本类型序列化和反序列化
TypeDef.h源码
#pragma once
#include "stdafx.h"
typedef bool Bool;
typedef unsigned char Byte;
typedef char SByte;
typedef short Int16;
typedef unsigned short UInt16;
typedef int Int32;
typedef unsigned int UInt32;
typedef long long Int64;
typedef unsigned long long UInt64;
typedef float Float;
typedef double Double;
typedef char* CString;
BinaryReader.h源码
#pragma once
#include "stdafx.h"
#include "TypeDef.h"
#include <string>
using namespace std;
class BinaryReader
{
private:
Byte* m_byteArray;
Int32 m_byteArrayLen;
Int32 m_position;
Int32 Read7BitEncodedInt();
public:
BinaryReader(Byte* byteArray=NULL, Int32 len=0);
Bool ReadBoolean();
Byte ReadByte();
Int16 ReadInt16();
UInt16 ReadUInt16();
Int32 ReadInt32();
UInt32 ReadUInt32();
Int64 ReadInt64();
UInt64 ReadUInt64();
CString ReadCString();
string ReadString();
Float ReadSingle();
Double ReadDouble();
Int32 GetPosition();
void SetPosition(Int32 v);
void Reset(Byte* byteArray, int byteArrayLen);
};
BinaryReader.cpp源码
#include "stdafx.h"
#include "BinaryReader.h"
#include <string>
using namespace std;
BinaryReader::BinaryReader(Byte* byteArray, Int32 byteLen)
{
m_byteArray = byteArray;
m_byteArrayLen = byteLen;
m_position = 0;
}
Int32 BinaryReader::GetPosition()
{
return m_position;
}
void BinaryReader::SetPosition(Int32 v)
{
if (v > m_byteArrayLen || v < 0)
{
return;
}
m_position = v;
}
void BinaryReader::Reset(Byte* byteArray,int byteArrayLen)
{
m_byteArray = byteArray;
m_byteArrayLen = byteArrayLen;
m_position = 0;
}
bool BinaryReader::ReadBoolean()
{
return (m_byteArray[m_position++] != 0);
}
Byte BinaryReader::ReadByte()
{
return m_byteArray[m_position++];
}
Int16 BinaryReader::ReadInt16()
{
Int16 v = (Int16)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8);
m_position += 2;
return v;
}
UInt16 BinaryReader::ReadUInt16()
{
UInt16 v = (UInt16)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8);
m_position += 2;
return v;
}
Int32 BinaryReader::ReadInt32()
{
Int32 v = (Int32)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8
| m_byteArray[m_position+2] << 16 | m_byteArray[m_position+3] << 24);
m_position += 4;
return v;
}
UInt32 BinaryReader::ReadUInt32()
{
UInt32 v = (UInt32)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8
| m_byteArray[m_position+2] << 16 | m_byteArray[m_position+3] << 24);
m_position += 4;
return v;
}
Int64 BinaryReader::ReadInt64()
{
UInt32 lo = (UInt32)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8 |
m_byteArray[m_position+2] << 16 | m_byteArray[m_position+3] << 24);
UInt32 hi = (UInt32)(m_byteArray[m_position+4] | m_byteArray[m_position+5] << 8 |
m_byteArray[m_position+6] << 16 | m_byteArray[m_position+7] << 24);
m_position += 8;
return (Int64)((UInt64)hi) << 32 | lo;
}
UInt64 BinaryReader::ReadUInt64()
{
UInt32 lo = (UInt32)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8 |
m_byteArray[m_position+2] << 16 | m_byteArray[m_position+3] << 24);
UInt32 hi = (UInt32)(m_byteArray[m_position+4] | m_byteArray[m_position+5] << 8 |
m_byteArray[m_position+6] << 16 | m_byteArray[m_position+7] << 24);
m_position += 8;
return ((UInt64)hi) << 32 | lo;
}
Float BinaryReader::ReadSingle()
{
UInt32 tmpBuffer = (UInt32)(m_byteArray[m_position] | m_byteArray[m_position+1] << 8 |
m_byteArray[m_position+2] << 16 | m_byteArray[m_position+3] << 24);
m_position += 4;
return *((Float*)&tmpBuffer);
}
Double BinaryReader::ReadDouble()
{
UInt32 lo = (UInt32)(m_byteArray[m_position] | (m_byteArray[m_position + 1] << 8) |
(m_byteArray[m_position + 2] << 16) | (m_byteArray[m_position + 3] << 24));
UInt32 hi = (UInt32)(m_byteArray[m_position + 4] | (m_byteArray[m_position + 5 ] << 8) |
(m_byteArray[m_position + 6 ] << 16) | (m_byteArray[m_position + 7] << 24));
m_position += 8;
UInt64 tmpBuffer = (((UInt64)hi) << 32) | lo;
return *((Double*)&tmpBuffer);
}
CString BinaryReader::ReadCString()
{
Int32 len = Read7BitEncodedInt();
CString bytes = new SByte[len + 1];
memcpy(bytes, m_byteArray + m_position, len);
m_position += len;
bytes[len] = '\0';
return bytes;
}
string BinaryReader::ReadString()
{
SByte* cStr = ReadCString();
string s(cStr);
return s;
}
int BinaryReader::Read7BitEncodedInt()
{
Int32 count = 0;
Int32 shift = 0;
Byte b;
for (int i = 0; i<5; i++)
{
if (shift == 5 * 7)
return 0;
b = ReadByte();
count |= (b & 0x7F) << shift;
shift += 7;
if ((b & 0x80) == 0)
{
break;
}
}
return count;
}
BinaryWriter.h源码
#pragma once
#include "stdafx.h"
#include "TypeDef.h"
#include <string>
using namespace std;
class BinaryWriter
{
private:
Byte* byteArray;
int capacity;
int position;
void Write7BitEncodedInt(Int32 v);
public:
BinaryWriter(int capacity = 32);
Int32 GetPosition();
void SetPosition(Int32 v);
Int32 GetCapacity();
void SetCapacity(Int32 v);
void CheckCapacity(Int32 addCount);
void Write(Bool value);
void Write(Byte v);
void Write(Byte* buffer, Int32 len);
void Write(Double value);
void Write(Int16 value);
void Write(UInt16 value);
void Write(Int32 value);
void Write(UInt32 value);
void Write(Int64 v);
void Write(UInt64 v);
void Write(Float v);
void Write(string v);
void Write(CString v);
Byte* GetBytes();
Int32 GetByteLen();
};
BinaryWriter.h源码
#include "stdafx.h"
#include "BinaryWriter.h"
BinaryWriter::BinaryWriter(int capacity)
{
byteArray = new Byte[capacity];
this->capacity = capacity;
position = 0;
}
Int32 BinaryWriter::GetPosition()
{
return position;
}
void BinaryWriter::SetPosition(Int32 v)
{
if (v > capacity)
{
CheckCapacity(v - capacity);
}
position = v;
}
Int32 BinaryWriter::GetCapacity()
{
return capacity;
}
void BinaryWriter::SetCapacity(Int32 v)
{
if (v != capacity)
{
if (capacity < position)
{
return;
}
if (v > 0)
{
Byte* newBuffer = new Byte[v];
if (position > 0) memcpy(newBuffer, byteArray, position);
byteArray = newBuffer;
}
else {
byteArray = NULL;
}
capacity = v;
}
}
void BinaryWriter::CheckCapacity(Int32 addCount)
{
Int32 newCapacity = position + addCount;
if (newCapacity > capacity)
{
if (newCapacity < 256)
newCapacity = 256;
if (newCapacity < capacity * 2)
newCapacity = capacity * 2;
SetCapacity(newCapacity);
}
}
void BinaryWriter::Write(Bool value)
{
CheckCapacity(1);
byteArray[position++] = (Byte)(value ? 1 : 0);
}
void BinaryWriter::Write(Byte v)
{
CheckCapacity(1);
byteArray[position++] = v;
}
void BinaryWriter::Write(Byte* buffer, Int32 len)
{
if (buffer == NULL || len == 0)
{
return;
}
CheckCapacity(len);
memcpy(byteArray, buffer, len);
position += len;
}
void BinaryWriter::Write(Double value)
{
CheckCapacity(8);
UInt64 TmpValue = *(UInt64*)&value;
byteArray[position++] = (Byte)TmpValue;
byteArray[position++] = (Byte)(TmpValue >> 8);
byteArray[position++] = (Byte)(TmpValue >> 16);
byteArray[position++] = (Byte)(TmpValue >> 24);
byteArray[position++] = (Byte)(TmpValue >> 32);
byteArray[position++] = (Byte)(TmpValue >> 40);
byteArray[position++] = (Byte)(TmpValue >> 48);
byteArray[position++] = (Byte)(TmpValue >> 56);
}
void BinaryWriter::Write(Int16 value)
{
CheckCapacity(2);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
}
void BinaryWriter::Write(UInt16 value)
{
CheckCapacity(2);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
}
void BinaryWriter::Write(Int32 value)
{
CheckCapacity(4);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
byteArray[position++] = (Byte)(value >> 16);
byteArray[position++] = (Byte)(value >> 24);
}
void BinaryWriter::Write(UInt32 value)
{
CheckCapacity(4);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
byteArray[position++] = (Byte)(value >> 16);
byteArray[position++] = (Byte)(value >> 24);
}
void BinaryWriter::Write(Int64 v)
{
CheckCapacity(8);
byteArray[position++] = (Byte)v;
byteArray[position++] = (Byte)(v >> 8);
byteArray[position++] = (Byte)(v >> 16);
byteArray[position++] = (Byte)(v >> 24);
byteArray[position++] = (Byte)(v >> 32);
byteArray[position++] = (Byte)(v >> 40);
byteArray[position++] = (Byte)(v >> 48);
byteArray[position++] = (Byte)(v >> 56);
}
void BinaryWriter::Write(UInt64 v)
{
CheckCapacity(8);
byteArray[position++] = (Byte)v;
byteArray[position++] = (Byte)(v >> 8);
byteArray[position++] = (Byte)(v >> 16);
byteArray[position++] = (Byte)(v >> 24);
byteArray[position++] = (Byte)(v >> 32);
byteArray[position++] = (Byte)(v >> 40);
byteArray[position++] = (Byte)(v >> 48);
byteArray[position++] = (Byte)(v >> 56);
}
void BinaryWriter::Write(Float v)
{
CheckCapacity(4);
UInt32 tmpValue = *(UInt32*)&v;
byteArray[position++] = (Byte)tmpValue;
byteArray[position++] = (Byte)(tmpValue >> 8);
byteArray[position++] = (Byte)(tmpValue >> 16);
byteArray[position++] = (Byte)(tmpValue >> 24);
}
void BinaryWriter::Write(string v)
{
if (v.size() == 0)
{
Write7BitEncodedInt(0);
return;
}
const char* cStr = v.c_str();
Int32 len = v.size();
Write7BitEncodedInt(len);
CheckCapacity(len);
memcpy(byteArray+ position,cStr,len);
position += len;
}
void BinaryWriter::Write(CString v)
{
if (v == NULL)
{
Write7BitEncodedInt(0);
return;
}
int len = strlen((const char*)v);
Write7BitEncodedInt(len);
CheckCapacity(len);
memcpy(byteArray + position, v, len);
position += len;
}
void BinaryWriter::Write7BitEncodedInt(Int32 v)
{
while (v >= 0x80)
{
Write((Byte)(v | 0x80));
v >>= 7;
}
Write((Byte)v);
}
Byte* BinaryWriter::GetBytes()
{
Byte* bytes = new Byte[position];
memcpy(bytes, byteArray, position);
return bytes;
}
Int32 BinaryWriter::GetByteLen()
{
return position;
}
BinaryWriter.cpp源码
#include "stdafx.h"
#include "BinaryWriter.h"
BinaryWriter::BinaryWriter(int capacity)
{
byteArray = new Byte[capacity];
this->capacity = capacity;
position = 0;
}
Int32 BinaryWriter::GetPosition()
{
return position;
}
void BinaryWriter::SetPosition(Int32 v)
{
if (v > capacity)
{
CheckCapacity(v - capacity);
}
position = v;
}
Int32 BinaryWriter::GetCapacity()
{
return capacity;
}
void BinaryWriter::SetCapacity(Int32 v)
{
if (v != capacity)
{
if (capacity < position)
{
return;
}
if (v > 0)
{
Byte* newBuffer = new Byte[v];
if (position > 0) memcpy(newBuffer, byteArray, position);
byteArray = newBuffer;
}
else {
byteArray = NULL;
}
capacity = v;
}
}
void BinaryWriter::CheckCapacity(Int32 addCount)
{
Int32 newCapacity = position + addCount;
if (newCapacity > capacity)
{
if (newCapacity < 256)
newCapacity = 256;
if (newCapacity < capacity * 2)
newCapacity = capacity * 2;
SetCapacity(newCapacity);
}
}
void BinaryWriter::Write(Bool value)
{
CheckCapacity(1);
byteArray[position++] = (Byte)(value ? 1 : 0);
}
void BinaryWriter::Write(Byte v)
{
CheckCapacity(1);
byteArray[position++] = v;
}
void BinaryWriter::Write(Byte* buffer, Int32 len)
{
if (buffer == NULL || len == 0)
{
return;
}
CheckCapacity(len);
memcpy(byteArray, buffer, len);
position += len;
}
void BinaryWriter::Write(Double value)
{
CheckCapacity(8);
UInt64 TmpValue = *(UInt64*)&value;
byteArray[position++] = (Byte)TmpValue;
byteArray[position++] = (Byte)(TmpValue >> 8);
byteArray[position++] = (Byte)(TmpValue >> 16);
byteArray[position++] = (Byte)(TmpValue >> 24);
byteArray[position++] = (Byte)(TmpValue >> 32);
byteArray[position++] = (Byte)(TmpValue >> 40);
byteArray[position++] = (Byte)(TmpValue >> 48);
byteArray[position++] = (Byte)(TmpValue >> 56);
}
void BinaryWriter::Write(Int16 value)
{
CheckCapacity(2);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
}
void BinaryWriter::Write(UInt16 value)
{
CheckCapacity(2);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
}
void BinaryWriter::Write(Int32 value)
{
CheckCapacity(4);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
byteArray[position++] = (Byte)(value >> 16);
byteArray[position++] = (Byte)(value >> 24);
}
void BinaryWriter::Write(UInt32 value)
{
CheckCapacity(4);
byteArray[position++] = (Byte)value;
byteArray[position++] = (Byte)(value >> 8);
byteArray[position++] = (Byte)(value >> 16);
byteArray[position++] = (Byte)(value >> 24);
}
void BinaryWriter::Write(Int64 v)
{
CheckCapacity(8);
byteArray[position++] = (Byte)v;
byteArray[position++] = (Byte)(v >> 8);
byteArray[position++] = (Byte)(v >> 16);
byteArray[position++] = (Byte)(v >> 24);
byteArray[position++] = (Byte)(v >> 32);
byteArray[position++] = (Byte)(v >> 40);
byteArray[position++] = (Byte)(v >> 48);
byteArray[position++] = (Byte)(v >> 56);
}
void BinaryWriter::Write(UInt64 v)
{
CheckCapacity(8);
byteArray[position++] = (Byte)v;
byteArray[position++] = (Byte)(v >> 8);
byteArray[position++] = (Byte)(v >> 16);
byteArray[position++] = (Byte)(v >> 24);
byteArray[position++] = (Byte)(v >> 32);
byteArray[position++] = (Byte)(v >> 40);
byteArray[position++] = (Byte)(v >> 48);
byteArray[position++] = (Byte)(v >> 56);
}
void BinaryWriter::Write(Float v)
{
CheckCapacity(4);
UInt32 tmpValue = *(UInt32*)&v;
byteArray[position++] = (Byte)tmpValue;
byteArray[position++] = (Byte)(tmpValue >> 8);
byteArray[position++] = (Byte)(tmpValue >> 16);
byteArray[position++] = (Byte)(tmpValue >> 24);
}
void BinaryWriter::Write(string v)
{
if (v.size() == 0)
{
Write7BitEncodedInt(0);
return;
}
const char* cStr = v.c_str();
Int32 len = v.size();
Write7BitEncodedInt(len);
CheckCapacity(len);
memcpy(byteArray+ position,cStr,len);
position += len;
}
void BinaryWriter::Write(CString v)
{
if (v == NULL)
{
Write7BitEncodedInt(0);
return;
}
int len = strlen((const char*)v);
Write7BitEncodedInt(len);
CheckCapacity(len);
memcpy(byteArray + position, v, len);
position += len;
}
void BinaryWriter::Write7BitEncodedInt(Int32 v)
{
while (v >= 0x80)
{
Write((Byte)(v | 0x80));
v >>= 7;
}
Write((Byte)v);
}
Byte* BinaryWriter::GetBytes()
{
Byte* bytes = new Byte[position];
memcpy(bytes, byteArray, position);
return bytes;
}
Int32 BinaryWriter::GetByteLen()
{
return position;
}