zoukankan      html  css  js  c++  java
  • [手游新项目历程]第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");
    } 

  • 相关阅读:
    ios开发之--把秒转换为天时分秒
    网络爬虫的类型
    网络爬虫的组成
    为什么要学网络爬虫
    什么是网络爬虫
    Windows 下安装 Python3
    Linux 下安装 Python3
    HTTP 代理
    HTTP Cookies
    爬虫的基本原理
  • 原文地址:https://www.cnblogs.com/byfei/p/14104297.html
Copyright © 2011-2022 走看看