当前位置:首页 > 通信资讯 > 正文

c++ 取位(c++按位读取)

在我创业的一个项目中,为了节约网络带宽,因此在网络中传输数据需要实现紧凑存取,在国防,科研,航天,军工等多个领域其实也有类似的需求。
实现紧凑存取,不是按一个字节一个字节地存取,而是按位存取。比如一个字节,我们可以存储8个bool信息,废话少说,直接分享代码(备注:里面的代码算法值得优化)。

//以下为函数定义

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 /***********************************************************************/ /* 函数作用:从buffer读一个位 */ /* 参数pBuffer[in]:指定buffer */ /* 参数nStart[in]:指定位置 */ /* 参数nEnd[out]:返回结束位置 */ /* 参数retByte[out]:返回读取结果值 */ /* 返回:void */ /***********************************************************************/ void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte ); /***********************************************************************/ /* 函数作用:从指定buffer里读任意一段位置数据 */ /* 参数pBuffer[in]:指定buffer */ /* 参数nStart[in]:指定位置 */ /* 参数btLength[in]:读取长度 */ /* 参数nEnd[out]:返回结束位置 */ /* 参数retData[out]:返回读取结果值,支持任意数据类型 */ /* 返回:void */ /***********************************************************************/ template<typename T> void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData ); /***********************************************************************/ /* 函数作用:从指定buffer里读取一段字符串 */ /* 参数pBuffer[in]:指定buffer */ /* 参数nStart[in]:指定位置 */ /* 参数nCount[in]:字符串长度 */ /* 参数nEnd[out]:返回结束位置 */ /* 参数pRetData[out]:返回读取字符串结果 */ /* 返回:void */ /***********************************************************************/ void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData ); /***********************************************************************/ /* 函数作用:向buffer写一个位 */ /* 参数pBuffer[in]:指定buffer */ /* 参数btData[in]:需要写入的值 */ /* 参数nStart[in]:指定位置 */ /* 参数nEnd[out]:返回结束位置 */ /* 返回:void */ /***********************************************************************/ void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd ); /***********************************************************************/ /* 函数作用:向指定buffer里写入任意一段数据 */ /* 参数pBuffer[in]:指定buffer */ /* 参数tData[in]:需要写入的数据,支持任意数据类型 */ /* 参数nStart[in]:指定位置 */ /* 参数btLength[in]:读取长度 */ /* 参数nEnd[out]:返回结束位置 */ /* 返回:void */ /***********************************************************************/ template<typename T> void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd ); /***********************************************************************/ /* 函数作用:向指定buffer里写取一段字符串 */ /* 参数pBuffer[in]:指定buffer */ /* 参数pchar[in]:需要写入的字符串 */ /* 参数nStart[in]:指定位置 */ /* 参数nCount[in]:字符串长度 */ /* 参数nEnd[out]:返回结束位置 */ /* 返回:void */ /***********************************************************************/ void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd );

//以下为函数实现

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ 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, /* out */int& nEnd, /* out */ 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, /* out */int& nEnd, /* out */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, /* out */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, /* out */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, /* out */int& nEnd ) { for ( int nIndex=0; nIndex<nCount; nIndex++ ) { WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart); } nEnd = nStart; }

以上就是本文的全部内容,希望对大家的学习有所帮助。

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。