bitset崩溃
byte testBytes[5]={0x1,0x2,0x3,0x4,0x0}; //vc环境下
std::string strHelp((char*)testBytes);
#if 1
int nTemp=strHelp.length();//nTemp==4 ?
#endif
std::bitset<40> temp2(strHelp);//崩溃
template<class _Elem,
class _Tr,
class _Alloc>
explicit bitset(const basic_string<_Elem, _Tr, _Alloc>& _Str,
_BITSET_SIZE_TYPE _Pos = 0)
{// construct from [_Pos, ...) elements in string
_Construct(_Str, _Pos,
basic_string<_Elem, _Tr, _Alloc>::npos, (_Elem)'0');
}
最终导致
void _Xinv() const
{// report invalid string element in bitset conversion
_THROW(invalid_argument, "invalid bitset<N> char");
}
抛出异常
需求: 用bitset模版类去操作字节数组testBytes!
[解决办法]
byte testBytes[5]={0x1,0x2,0x3,0x4,0x0}; //vc环境下
std::string strHelp((char*)testBytes);
#if 1
int nTemp=strHelp.length(); //nTemp==4 ?
#endif
std::bitset<40> temp2(strHelp); //崩溃
byte testBytes[5]= {0x1,0x2,0x3,0x4,0x0};
std::bitset<40> temp2;
unsigned int n = 0;
for( unsigned int i = 0; i < 5; ++ i)
{
for( unsigned int j = 0; j < 8; ++ j)
{
temp2[n] = testBytes[i] >> j;
++ n;
}
}