[手游新项目历程]第7天-读取二进制的char*数据to整形

#include "Stream.h"
#include <Windows.h>
#include <iostream>
#include <string.h>
#include "md5.h"
#include <stdio.h>
using namespace std;

#include  <iostream>
#include  <string.h>

/*	字符串to整形	
int ReadUint8(Stream &stream)
{
	char pBuff[8];
	stream.ReadBuffer(pBuff,8);
	int i,len,sum=0;
	len=8;
	for(i=len-1;i>=0;i--)
		sum+=(long)(pBuff[i]-'0')<<(len-1-i);
	return sum;
}

void main()
{
	char* str = "1111111100000001";
	Stream stream(str,256);
	cout<< ReadUint8(stream)<<endl;
	cout<< ReadUint8(stream)<<endl;
	system("pause");
} 
*/


//------------------二进制to整形---------------------------
//获得某一位是为0还是1,1是最低位
int getBit(char data, int bit)
{
	bit = bit -1;
	int a=1;
	a<<=bit;
	data&=a;
	if(data == a)
	{
		return 1;
	}
	return 0;
}

//nSize 一次多多少个char 
int ReadUint8(Stream &stream,uint32 nSize)
{ 
	char *pBuff = new char[nSize];	
	stream.ReadBuffer(pBuff,nSize);
	int sum=0,seize = 0;
	for(int n=0;n<nSize;n++)
	{
		char cdata = pBuff[n];
		int data = (int)(cdata-48);
		cout<<"data="<<cdata<<endl;
		if(data != 0)
		{
			for(int i=1;i<=8;i++)
			//for(int i=8;i>=1;i--)//	如果是低位储存 for(int i=1;i<=8;i++)
			{			
				int bit = getBit(data, i);			
				int add = bit;
				if(add !=0)
					add<<=seize;
				sum+=add;
				seize++;
				cout<<add<<" ";
			}
		}
		cout<<endl;
	}
	return sum;
}

void main()
{
	char* str = "1111111111111";	//二个位
	Stream stream(str,256);
	cout<< ReadUint8(stream,2)<<endl;
	system("pause");
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值