65,187
社区成员




#include <string>
#include <vector>
using std::string;
using std::vector;
int Binary2Int(const string & item);
int main()
{
//4bit一组。
const int nItemSize = 4;
//读文件->strData
string strData("01100001010010101010");
string item;
vector<int> vcInteger;
for (string::size_type i=0; i<strData.size(); i+=nItemSize)
{
item = strData.substr(i,nItemSize);
vcInteger.push_back(Binary2Int(item));
}
//接下来解析vcInteger,计算数字出现的个数。 有了这个vcInteger,解析很简单
// ...
return 0;
}
int Binary2Int(const string & item)
{
int targ = 0;
//注意平台的大小端问题
targ += (item[3]=='1' ? 1 : 0);
targ += (item[2]=='1' ? 2 : 0);
targ += (item[1]=='1' ? 4 : 0);
targ += (item[0]=='1' ? 8 : 0);
return targ;
}
void example(){
struct read_type{
char str[4];
}readI0;
FILE * fp;
if((fp=fopen("d:\\1.txt","r"))==NULL)
{
printf("cant open the file");
exit(0);
}
int a[16]={0}; //够用了
while(fread(&readI0,sizeof(struct read_type),1,fp)){
// cout<<readI0.str[0];
// cout<<readI0.str[1];
// cout<<readI0.str[2];
// cout<<readI0.str[3];
// 转化为十进制 最大1111为15
int b=(readI0.str[0]-48)*2*2*2+(readI0.str[1]-48)*2*2+(readI0.str[2]-48)*2+(readI0.str[3]-48);
cout<<b<<endl;
a[b]++;
}
}