看过你的一个回答(http://expert.csdn.net/Expert/topic/2679/2679927.xml?temp=.3427088),我想向你请教一个问题:
BYTE buf[ 5 ];
buf <<= 6;
是不是buf[ 0 ]、buf[ 1 ]、...、buf[ 4 ]都左移了6位?
顺便讲一下,你所答问题我是这样做的:
#include <windows.h>
#include <assert.h>
#include <bitset>
#define BYTESIZE 8
using namespace std;DWORD DWordFromBits( const BYTE *buf, const int start, const int end )
// 将起始位置为buf的字节块从第start位到end位的二进制数据复制到双字中。
{
bool ByteIsSetInPos( const BYTE byte, const int pos ); if( !buf || end < start ||
end - start >= sizeof( DWORD ) * BYTESIZE )
return 0;
DWORD result = 0;
bitset< sizeof( DWORD ) * BYTESIZE > bits; // 构造32位位集bits。
int startPos, endPos; // 需要复制的每字节的起始、终止位置。
int setPos = end - start; // bits当前要复制的位置。
for( int i = start / BYTESIZE; i <= end / BYTESIZE; i ++ )
{
startPos = ( ( i * BYTESIZE ) < start )? ( start % BYTESIZE ) : 0;
endPos = ( ( i * BYTESIZE ) + 7 > end )? ( end % BYTESIZE ) : 7;
for( int bitPos = startPos; bitPos <= endPos; bitPos ++ )
bits[ setPos -- ] = ByteIsSetInPos( buf[ i ], bitPos );
}
result = bits.to_ulong( );
return result;
}bool ByteIsSetInPos( const BYTE byte, const int pos )
// 判断字节byte中的第pos位是否为1。
{
assert( pos >= 0 && pos <= 7 );
BYTE value = ( byte << pos );
value >>= 7;
return ( value? TRUE : FALSE );
}
BYTE buf[ 5 ];
buf <<= 6;
是不是buf[ 0 ]、buf[ 1 ]、...、buf[ 4 ]都左移了6位?
顺便讲一下,你所答问题我是这样做的:
#include <windows.h>
#include <assert.h>
#include <bitset>
#define BYTESIZE 8
using namespace std;DWORD DWordFromBits( const BYTE *buf, const int start, const int end )
// 将起始位置为buf的字节块从第start位到end位的二进制数据复制到双字中。
{
bool ByteIsSetInPos( const BYTE byte, const int pos ); if( !buf || end < start ||
end - start >= sizeof( DWORD ) * BYTESIZE )
return 0;
DWORD result = 0;
bitset< sizeof( DWORD ) * BYTESIZE > bits; // 构造32位位集bits。
int startPos, endPos; // 需要复制的每字节的起始、终止位置。
int setPos = end - start; // bits当前要复制的位置。
for( int i = start / BYTESIZE; i <= end / BYTESIZE; i ++ )
{
startPos = ( ( i * BYTESIZE ) < start )? ( start % BYTESIZE ) : 0;
endPos = ( ( i * BYTESIZE ) + 7 > end )? ( end % BYTESIZE ) : 7;
for( int bitPos = startPos; bitPos <= endPos; bitPos ++ )
bits[ setPos -- ] = ByteIsSetInPos( buf[ i ], bitPos );
}
result = bits.to_ulong( );
return result;
}bool ByteIsSetInPos( const BYTE byte, const int pos )
// 判断字节byte中的第pos位是否为1。
{
assert( pos >= 0 && pos <= 7 );
BYTE value = ( byte << pos );
value >>= 7;
return ( value? TRUE : FALSE );
}
那道题我答错了
(DWORD)(Buf<<startpos>>startpos>>(size*8-1-endpos))
不能上面这样写(DWORD)(*Buf<<startpos>>startpos>>(size*8-1-endpos))
(DWORD)(Buf[0]<<startpos>>startpos>>(size*8-1-endpos))
还可以
不过这只能对某一个BYTE进行操作
不是所要的答案
当时没有测试,实在不好意思
电脑没有VC
#include <Classes.hpp>class TByteOper
{
public:
TByteOper();
TByteOper(BYTE *pBytes,int nCount);
TByteOper& operator >>(const int nSize);
TByteOper& operator <<(const int nSize);
TByteOper& operator ^(TByteOper* pOper);
TByteOper& operator ^(TByteOper &Oper);
void ResetContent();
TByteOper& SetNewByte(BYTE * pBytes, int nCount);
__property BYTE* pByteArrays = { read=GetpByteArrays};
__property int nByteCount = { read=GetnByteCount};
private:
BYTE* __fastcall GetpByteArrays();
int __fastcall GetnByteCount();
int nCount;
BYTE *pBytes;};
cpp文件
#include "byteOper.h"
#include "Dialogs.hpp"
TByteOper::TByteOper()
{
this->pBytes=NULL;
this->nCount=0;
}
TByteOper::TByteOper(BYTE *pBytes,int nCount)
{
this->pBytes=new BYTE[nCount];
memcpy(this->pBytes,pBytes,nCount);
this->nCount=nCount;
}BYTE* __fastcall TByteOper::GetpByteArrays()
{
return this->pBytes;
//TODO: Add your source code here
}
int __fastcall TByteOper::GetnByteCount()
{
return this->nCount;
//TODO: Add your source code here
}TByteOper& TByteOper:: operator >>(const int nSize)
{
BYTE byRight,byLeft,byDes;
byLeft=byRight=byDes=0;
BYTE *byBackup=new BYTE[nCount];
memcpy(byBackup,pBytes,nCount);
int j=0;
for(int i=0;i<nCount;i++)
{
byDes=0;
if((i+1)*8>nSize)
{
byRight=byBackup[j];
byLeft=byBackup[j-1];
j++;
byDes|=byLeft<<8-nSize%8; //左边
byDes|=byRight>>nSize%8; //右边
} pBytes[i]=byDes;
}
delete byBackup;
return *this;
}
TByteOper& TByteOper:: operator << (const int nSize)
{
BYTE byRight,byLeft,byDes;
byLeft=byRight=byDes=0;
BYTE *byBackup=new BYTE[nCount];
memcpy(byBackup,pBytes,nCount);
int j=nCount-1;
for(int i=0;i<nCount;i++)
{
byDes=0;
if((i+1)*8>nSize)
{
byLeft=byBackup[j-1];
byRight=byBackup[j];
j--;
byDes|=byLeft>>8-nSize%8; //左边
byDes|=byRight<<nSize%8; //右边
} pBytes[nCount-i-1]=byDes;
}
delete byBackup;
return *this;
}
TByteOper& TByteOper:: operator ^ (TByteOper* pOper)
{
if(!pOper||this->nCount!=(pOper->nByteCount))
return *this;//NULL;
for(int i=0;i<pOper->nByteCount;i++)
this->pBytes[i]^=pOper->pByteArrays[i];
return *this;
}
TByteOper& TByteOper:: operator ^ (TByteOper &Oper)
{
if(this->nCount!=(Oper.nByteCount))
return *this;//NULL;
for(int i=0;i<Oper.nByteCount;i++)
this->pBytes[i]^=Oper.pByteArrays[i];
return *this;
}void TByteOper::ResetContent()
{
if(pBytes)
delete this->pBytes;
this->pBytes=NULL;
this->nCount=0;
}TByteOper& TByteOper::SetNewByte(BYTE * pBytes, int nCount)
{
ResetContent();
this->pBytes=new BYTE[nCount];
memcpy(this->pBytes,pBytes,nCount);
this->nCount=nCount;
return *this;
}
上面的类,作用是可以整端移位,
再加上
(DWORD)(Buf<<startpos>>startpos>>(size*8-1-endpos))
的代码,就可以得到BoweirrKing(忽然变傻) 需要的代码了
BYTE *Buf=oper.pByteArrays;
(DWORD)(Buf<<startpos>>startpos>>(size*8-1-endpos))
自己改一下在VC下面使用,
调用的时候
BYTE buf[3]={75,214,213};
TByteOper oper(buf,3);
BYTE buf2[3]={75,41,213};
TByteOper *oper2=new TByteOper(buf2,3);
oper<<8>>16<<8;
*oper2<<8>>16<<8;
// B *b=(B*)buf;
//AnsiString str2(sizeof(WORD));
//ShowMessage(str2);
//buf<<=2;
//DWORD dw=(*buf<<0>>0>>7);
oper^oper2;
AnsiString str(buf[1]);
ShowMessage(str);
delete oper2;
还有好些可以添加的,修改的地方,上了网顺便就发了
有什么问题,欢迎指出^_^
DWORD dw=(DWORD)pB;