首页 > 编程 > C++ > 正文

如何在C++中实现按位存取

2020-05-23 14:17:00
字体:
来源:转载
供稿:网友

实现紧凑存取,不是按一个字节一个字节地存取,而是按位存取,本文就是介绍了如何在C++中实现按位存取,需要的朋友可以参考下

在我创业的一个项目中,为了节约网络带宽,因此在网络中传输数据需要实现紧凑存取,在国防,科研,航天,军工等多个领域其实也有类似的需求。

实现紧凑存取,不是按一个字节一个字节地存取,而是按位存取。比如一个字节,我们可以存储8个bool信息,废话少说,直接分享代码(备注:里面的代码算法值得优化)。

//以下为函数定义

 

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

//以下为函数实现

 

 
  1. void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte )  
  2. {  
  3. byte btData = pBuffer[nStart/8];  
  4. btData = btData << nStart%8;  
  5. retByte = btData >> 7;  
  6. nEnd = nStart+1;  
  7. }  
  8.  
  9. template<typename T>  
  10. void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData )  
  11. {  
  12. //顺序读位  
  13. retData = 0;  
  14. if ( btLength > sizeof(T)*8 )  
  15. return ;  
  16.  
  17. byte btData;  
  18. T tData;  
  19. while ( btLength-- )  
  20. {  
  21. ReadOneBit(pBuffer, nStart, nStart, btData);  
  22. tData = btData << btLength;  
  23. retData |= tData;  
  24. }  
  25.  
  26. nEnd = nStart;  
  27. }  
  28.  
  29. void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData )  
  30. {  
  31. for ( int nIndex=0; nIndex<nCount; nIndex++ )  
  32. {  
  33. ReadDataFromBuffer(pBuffer, nStart, 8, nStart, pRetData[nIndex]);  
  34. }  
  35. nEnd = nStart;  
  36. }  
  37.  
  38.  
  39. void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd )  
  40. {  
  41. int nSet = nStart / 8;  
  42. byte c = pBuffer[nSet];  
  43. switch ( btData )  
  44. {  
  45. case 1:  
  46. c |= ( 1 << (7- nStart % 8) );  
  47. break;  
  48. case 0:  
  49. c &= ( ~(1 << (7- nStart % 8) ) );  
  50. break;  
  51. default:  
  52. return;  
  53. }  
  54. pBuffer [nSet] = c;  
  55. nEnd = nStart +1;  
  56. }  
  57.  
  58.  
  59.  
  60. template<typename T>  
  61. void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd )  
  62. {  
  63. /* //大端机模式  
  64. byte btDataLength = sizeof(T);  
  65. if ( btLength > sizeof(T)*8 )  
  66. return;  
  67.  
  68. int nDataStart = 0; //数据的第一位位置为0,顺序写入  
  69. while ( btLength-- )  
  70.  
  71. byte bitData;  
  72. ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData);  
  73. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  74.  
  75.  
  76. nEnd = nStart;  
  77. */ 
  78.  
  79. //小端机模式:写buffer的时候,不能顺序写位  
  80.  
  81. //获得模版占用字节大小  
  82. byte btDataLength = sizeof(T);  
  83.  
  84. //校验长度是否越界  
  85. if ( btLength > sizeof(T)*8 )  
  86. return;  
  87.  
  88. //将待写数据转为byte*  
  89. byte* ptData = (byte*)&tData;  
  90.  
  91. //求模与余  
  92. int nSet = btLength / 8;  
  93. int nRin = btLength % 8;  
  94.  
  95. //定义字节数据与位数据  
  96. byte bitData;  
  97. byte byteData;  
  98. int nTempEnd;  
  99.  
  100. //先写rin数据  
  101. byteData = ptData[nSet];  
  102. while ( nRin-- )  
  103. {  
  104. ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData);  
  105. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  106. }  
  107.  
  108. //再写Set数据  
  109. while ( nSet )  
  110. {  
  111. byteData = ptData[--nSet];  
  112. //写一个byte  
  113. int i=0;  
  114. while ( i!=8 )  
  115. {  
  116. ReadOneBit(&byteData, i++, nTempEnd, bitData);  
  117. WriteOneBit(pBuffer, bitData, nStart, nStart);  
  118. }  
  119. }  
  120. nEnd = nStart;  
  121.  
  122. }  
  123.  
  124.  
  125. void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd )  
  126. {  
  127. for ( int nIndex=0; nIndex<nCount; nIndex++ )  
  128. {  
  129. WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart);  
  130. }  
  131. nEnd = nStart;  
  132. }  

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表