最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

如何在C++中實(shí)現(xiàn)按位存取

 更新時(shí)間:2022年04月27日 14:54:16   作者:VeiwoZouhui  
實(shí)現(xiàn)緊湊存取,不是按一個(gè)字節(jié)一個(gè)字節(jié)地存取,而是按位存取,本文就是介紹了如何在C++中實(shí)現(xiàn)按位存取,需要的朋友可以參考下

在我創(chuàng)業(yè)的一個(gè)項(xiàng)目中,為了節(jié)約網(wǎng)絡(luò)帶寬,因此在網(wǎng)絡(luò)中傳輸數(shù)據(jù)需要實(shí)現(xiàn)緊湊存取,在國(guó)防,科研,航天,軍工等多個(gè)領(lǐng)域其實(shí)也有類似的需求。
實(shí)現(xiàn)緊湊存取,不是按一個(gè)字節(jié)一個(gè)字節(jié)地存取,而是按位存取。比如一個(gè)字節(jié),我們可以存儲(chǔ)8個(gè)bool信息,廢話少說(shuō),直接分享代碼(備注:里面的代碼算法值得優(yōu)化)。

//以下為函數(shù)定義 

/***********************************************************************/ 
/*  函數(shù)作用:從buffer讀一個(gè)位                    */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  參數(shù)retByte[out]:返回讀取結(jié)果值                 */ 
/*  返回:void                              */ 
/***********************************************************************/ 
void ReadOneBit( byte* pBuffer, int nStart, /* out */int& nEnd, /* out */ byte& retByte ); 
 
/***********************************************************************/ 
/*  函數(shù)作用:從指定buffer里讀任意一段位置數(shù)據(jù)            */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)btLength[in]:讀取長(zhǎng)度                    */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  參數(shù)retData[out]:返回讀取結(jié)果值,支持任意數(shù)據(jù)類型        */ 
/*  返回:void                              */ 
/***********************************************************************/ 
template<typename T> 
void ReadDataFromBuffer( byte* pBuffer, int nStart, byte btLength, /* out */int& nEnd, /* out */ T& retData ); 
 
/***********************************************************************/ 
/*  函數(shù)作用:從指定buffer里讀取一段字符串              */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)nCount[in]:字符串長(zhǎng)度                    */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  參數(shù)pRetData[out]:返回讀取字符串結(jié)果               */ 
/*  返回:void                              */ 
/***********************************************************************/ 
void ReadStringFromBuffer( byte* pBuffer, int nStart, int nCount, /* out */int& nEnd, /* out */char* pRetData ); 
 
 
 
/***********************************************************************/ 
/*  函數(shù)作用:向buffer寫一個(gè)位                    */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)btData[in]:需要寫入的值                   */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  返回:void                              */ 
/***********************************************************************/ 
void WriteOneBit( byte* pBuffer, byte btData, int nStart, /* out */int& nEnd ); 
 
/***********************************************************************/ 
/*  函數(shù)作用:向指定buffer里寫入任意一段數(shù)據(jù)             */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)tData[in]:需要寫入的數(shù)據(jù),支持任意數(shù)據(jù)類型          */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)btLength[in]:讀取長(zhǎng)度                    */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  返回:void                              */ 
/***********************************************************************/ 
template<typename T> 
void WriteDataToBuffer( byte* pBuffer, T tData, int nStart, byte btLength, /* out */int& nEnd ); 
 
/***********************************************************************/ 
/*  函數(shù)作用:向指定buffer里寫取一段字符串              */ 
/*  參數(shù)pBuffer[in]:指定buffer                    */ 
/*  參數(shù)pchar[in]:需要寫入的字符串                  */ 
/*  參數(shù)nStart[in]:指定位置                     */ 
/*  參數(shù)nCount[in]:字符串長(zhǎng)度                    */ 
/*  參數(shù)nEnd[out]:返回結(jié)束位置                    */ 
/*  返回:void                              */ 
/***********************************************************************/ 
void WtriteStringToBuffer( byte* pBuffer, char* pchar, int nStart, int nCount, /* out */int& nEnd ); 

//以下為函數(shù)實(shí)現(xiàn)

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 ) 
{ 
/* //大端機(jī)模式 
  byte btDataLength = sizeof(T); 
  if ( btLength > sizeof(T)*8 ) 
    return; 
   
  int nDataStart = 0; //數(shù)據(jù)的第一位位置為0,順序?qū)懭?
  while ( btLength-- ) 
  { 
    byte bitData; 
    ReadOneBit((byte*)&tData, nDataStart, nDataStart, bitData); 
    WriteOneBit(pBuffer, bitData, nStart, nStart); 
  } 
   
  nEnd = nStart; 
*/ 
 
  //小端機(jī)模式:寫buffer的時(shí)候,不能順序?qū)懳?
 
  //獲得模版占用字節(jié)大小 
  byte btDataLength = sizeof(T); 
 
  //校驗(yàn)長(zhǎng)度是否越界 
  if ( btLength > sizeof(T)*8 ) 
    return; 
 
  //將待寫數(shù)據(jù)轉(zhuǎn)為byte* 
  byte* ptData = (byte*)&tData;  
 
  //求模與余 
  int nSet = btLength / 8; 
  int nRin = btLength % 8; 
   
  //定義字節(jié)數(shù)據(jù)與位數(shù)據(jù) 
  byte bitData; 
  byte byteData; 
  int nTempEnd; 
 
  //先寫rin數(shù)據(jù) 
  byteData = ptData[nSet]; 
  while ( nRin-- ) 
  { 
    ReadOneBit(&byteData, 7-nRin, nTempEnd, bitData); 
    WriteOneBit(pBuffer, bitData, nStart, nStart); 
  } 
 
  //再寫Set數(shù)據(jù) 
  while ( nSet ) 
  { 
    byteData = ptData[--nSet]; 
    //寫一個(gè)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; 
} 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論

漾濞| 二手房| 尼玛县| 新田县| 三都| 乐平市| 苍溪县| 海城市| 资兴市| 泽库县| 万全县| 扬州市| 买车| 满洲里市| 田阳县| 乡城县| 江西省| 云龙县| 郯城县| 都兰县| 大埔区| 梨树县| 客服| 榆林市| 西乡县| 毕节市| 株洲市| 宁武县| 犍为县| 绍兴县| 洛阳市| 伊通| 玉林市| 高青县| 龙海市| 怀远县| 洛南县| 治县。| 边坝县| 高台县| 沂水县|