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");
    } 

  • 相关阅读:
    hdu 1042 N!
    hdu 1002 A + B Problem II
    c++大数模板
    hdu 1004 Let the Balloon Rise
    hdu 4027 Can you answer these queries?
    poj 2823 Sliding Window
    hdu 3074 Multiply game
    hdu 1394 Minimum Inversion Number
    hdu 5199 Gunner
    九度oj 1521 二叉树的镜像
  • 原文地址:https://www.cnblogs.com/byfei/p/14104297.html
Copyright © 2011-2022 走看看