为了节约网络带宽,因此在网络中传输数据需要实现紧凑存取.所谓实现紧凑存取,不是按一个字节一个字节地存取,而是按位存取。比如一个字节,我们可以存储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. /*   函数作用:从指定buffer里读任意一段位置数据                        */
  12. /*   参数pBuffer[in]:指定buffer                                       */
  13. /*   参数nStart[in]:指定位置                                          */
  14. /*   参数btLength[in]:读取长度                                        */
  15. /*   参数nEnd[out]:返回结束位置                                       */
  16. /*   参数retData[out]:返回读取结果值,支持任意数据类型                */
  17. /*   返回:void                                                           */
  18. /***********************************************************************/
  19. template<typename T> 
  20. void  ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData ); 
  21. /***********************************************************************/
  22. /*   函数作用:从指定buffer里读取一段字符串                            */
  23. /*   参数pBuffer[in]:指定buffer                                       */
  24. /*   参数nStart[in]:指定位置                                          */
  25. /*   参数nCount[in]:字符串长度                                        */
  26. /*   参数nEnd[out]:返回结束位置                                       */
  27. /*   参数pRetData[out]:返回读取字符串结果                             */
  28. /*   返回:void                                                           */
  29. /***********************************************************************/
  30. void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData ); 
  31. /***********************************************************************/
  32. /*   函数作用:向buffer写一个位                                        */
  33. /*   参数pBuffer[in]:指定buffer                                       */
  34. /*   参数btData[in]:需要写入的值                                      */
  35. /*   参数nStart[in]:指定位置                                          */
  36. /*   参数nEnd[out]:返回结束位置                                       */
  37. /*   返回:void                                                           */
  38. /***********************************************************************/
  39. void WriteOneBit( byte* pBuffer, byte btData, int nStart,  /* out */int& nEnd ); 
  40. /***********************************************************************/
  41. /*   函数作用:向指定buffer里写入任意一段数据                          */
  42. /*   参数pBuffer[in]:指定buffer                                       */
  43. /*   参数tData[in]:需要写入的数据,支持任意数据类型                   */
  44. /*   参数nStart[in]:指定位置                                          */
  45. /*   参数btLength[in]:读取长度                                        */
  46. /*   参数nEnd[out]:返回结束位置                                       */
  47. /*   返回:void                                                           */
  48. /***********************************************************************/
  49. template<typename T> 
  50. void  WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd ); 
  51. /***********************************************************************/
  52. /*   函数作用:向指定buffer里写取一段字符串                            */
  53. /*   参数pBuffer[in]:指定buffer                                       */
  54. /*   参数pchar[in]:需要写入的字符串                                   */
  55. /*   参数nStart[in]:指定位置                                          */
  56. /*   参数nCount[in]:字符串长度                                        */
  57. /*   参数nEnd[out]:返回结束位置                                       */
  58. /*   返回:void                                                           */
  59. /***********************************************************************/
  60. 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.     byte btData = pBuffer[nStart/8]; 
  3.     btData = btData << nStart%8; 
  4.     retByte = btData >> 7; 
  5.     nEnd = nStart+1; 
  6. template<typename T> 
  7. void  ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData ) 
  8. //顺序读位
  9.     retData = 0; 
  10. if ( btLength > sizeof(T)*8 ) 
  11. return ; 
  12.     byte btData; 
  13.     T tData; 
  14. while ( btLength– ) 
  15.     { 
  16.         ReadOneBit(pBuffer, nStart, nStart, btData); 
  17.         tData = btData << btLength; 
  18.         retData |= tData; 
  19.     } 
  20.     nEnd = nStart; 
  21. void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData ) 
  22. for ( int nIndex=0; nIndex<nCount; nIndex++ ) 
  23.     { 
  24.         ReadDataFromBuffer(pBuffer, nStart, 8, nStart, pRetData[nIndex]); 
  25.     } 
  26.     nEnd = nStart; 
  27. void WriteOneBit( byte* pBuffer, byte btData, int nStart,  /* out */int& nEnd ) 
  28. int nSet = nStart / 8; 
  29.     byte c = pBuffer[nSet]; 
  30. switch ( btData ) 
  31.     { 
  32. case 1: 
  33.         c |= ( 1 << (7- nStart % 8) ); 
  34. break; 
  35. case 0: 
  36.         c &= ( ~(1 << (7- nStart % 8) ) ); 
  37. break; 
  38. default: 
  39. return; 
  40.     } 
  41.     pBuffer [nSet] = c; 
  42.     nEnd = nStart +1; 
  43. template<typename T> 
  44. void  WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd ) 
  45. /* //大端机模式
  46.     byte btDataLength = sizeof(T);
  47.     if ( btLength > sizeof(T)*8 )
  48.         return;
  49.     int nDataStart = 0; //数据的第一位位置为0,顺序写入
  50.     while ( btLength– )
  51.     {
  52.         byte bitData;
  53.         ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData);
  54.         WriteOneBit(pBuffer, bitData, nStart, nStart);
  55.     }
  56.     nEnd = nStart;
  57. */
  58. //小端机模式:写buffer的时候,不能顺序写位
  59. //获得模版占用字节大小
  60.     byte btDataLength = sizeof(T); 
  61. //校验长度是否越界
  62. if ( btLength > sizeof(T)*8 ) 
  63. return; 
  64. //将待写数据转为byte*
  65.     byte* ptData = (byte*)&tData;  
  66. //求模与余
  67. int nSet = btLength / 8; 
  68. int nRin = btLength % 8; 
  69. //定义字节数据与位数据
  70.     byte bitData; 
  71.     byte byteData; 
  72. int nTempEnd; 
  73. //先写rin数据
  74.     byteData = ptData[nSet]; 
  75. while ( nRin– ) 
  76.     { 
  77.         ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData); 
  78.         WriteOneBit(pBuffer, bitData, nStart, nStart); 
  79.     } 
  80. //再写Set数据
  81. while ( nSet ) 
  82.     { 
  83.         byteData = ptData[–nSet]; 
  84. //写一个byte
  85. int i=0; 
  86. while ( i!=8 ) 
  87.         { 
  88.             ReadOneBit(&byteData, i++, nTempEnd, bitData); 
  89.             WriteOneBit(pBuffer, bitData, nStart, nStart); 
  90.         } 
  91.     } 
  92.     nEnd = nStart; 
  93. void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart,  int nCount, /* out */int& nEnd  ) 
  94. for ( int nIndex=0; nIndex<nCount; nIndex++ ) 
  95.     { 
  96.         WriteDataToBuffer(pBuffer, pchar[nIndex], nStart, 8, nStart); 
  97.     } 
  98.     nEnd = nStart; 
  99. }

评论被关闭。