在我创业的一个项目中,为了节约网络带宽,因此在网络中传输数据需要实现紧凑存取,在国防,科研,航天,军工等多个领域其实也有类似的需求。
实现紧凑存取,不是按一个字节一个字节地存取,而是按位存取。比如一个字节,我们可以存储8个bool信息,废话少说,直接分享代码(备注:里面的代码算法值得优化)。
//以下为函数定义
/***********************************************************************/ /***********************************************************************/ void ReadOneBit( byte* pBuffer, int nStart, int& nEnd, byte& retByte ); /***********************************************************************/ /***********************************************************************/ template<typename T> void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, int& nEnd, T& retData ); /***********************************************************************/ /***********************************************************************/ void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, int& nEnd, char* pRetData ); /***********************************************************************/ /***********************************************************************/ void WriteOneBit( byte* pBuffer, byte btData, int nStart, int& nEnd ); /***********************************************************************/ /***********************************************************************/ template<typename T> void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, int& nEnd ); /***********************************************************************/ /***********************************************************************/ void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, int& nEnd );
//以下为函数实现
void ReadOneBit( byte* pBuffer, int nStart, int& nEnd, byte& retByte ) { byte btData = pBuffer[nStart/8]; btData = btData << nStart%8; retByte = btData >> 7; nEnd = nStart+1; } template<typename T> void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, int& nEnd, T& retData ) { //顺序读位 retData = 0; if ( btLength > sizeof(T)*8 ) return ; byte btData; T tData; while ( btLength-- ) { ReadOneBit(pBuffer, nStart, nStart, btData); tData = btData << btLength; retData |= tData; } nEnd = nStart; } void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, int& nEnd, char* pRetData ) { for ( int nIndex=0; nIndex<nCount; nIndex++ ) { ReadDataFromBuffer(pBuffer, nStart, 8, nStart, pRetData[nIndex]); } nEnd = nStart; } void WriteOneBit( byte* pBuffer, byte btData, int nStart, int& nEnd ) { int nSet = nStart / 8; byte c = pBuffer[nSet]; switch ( btData ) { case 1: c |= ( 1 << (7- nStart % 8) ); break; case 0: c &= ( ~(1 << (7- nStart % 8) ) ); break; default: return; } pBuffer [nSet] = c; nEnd = nStart +1; } template<typename T> void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, int& nEnd ) { /* //大端机模式 byte btDataLength = sizeof(T); if ( btLength > sizeof(T)*8 ) return; int nDataStart = 0; //数据的第一位位置为0,顺序写入 while ( btLength-- ) { byte bitData; ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData); WriteOneBit(pBuffer, bitData, nStart, nStart); } nEnd = nStart; */ //小端机模式:写buffer的时候,不能顺序写位 //获得模版占用字节大小 byte btDataLength = sizeof(T); //校验长度是否越界 if ( btLength > sizeof(T)*8 ) return; //将待写数据转为byte* byte* ptData = (byte*)&tData; //求模与余 int nSet = btLength / 8; int nRin = btLength % 8; //定义字节数据与位数据 byte bitData; byte byteData; int nTempEnd; //先写rin数据 byteData = ptData[nSet]; while ( nRin-- ) { ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData); WriteOneBit(pBuffer, bitData, nStart, nStart); } //再写Set数据 while ( nSet ) { byteData = ptData[--nSet]; //写一个byte int i=0; while ( i!=8 ) { ReadOneBit(&byteData, i++, nTempEnd, bitData); WriteOneBit(pBuffer, bitData, nStart, nStart); } } nEnd = nStart; } void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, int& nEnd ) { for ( int nIndex=0; nIndex<nCount; nIndex++ ) { WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart); } nEnd = nStart; }
以上就是本文的全部内容,希望对大家的学习有所帮助。