一个将任意字符或字符串按位转化为整数的方法

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <string.h>
using namespace std;

int main()
{
    char  p[1024];
    cin>>p;
    int len = strlen(p);
    int i,j;
    unsigned val;
    long long  sum =0;
    int k = 0;
    for(i = len-1; i>=0; i--)
    {
            for(j = 0; j<8; j++)
            {
                    val = p[i]>>j;
                    val&=0x01;
                    sum = sum +val*pow(2,k);
                    k++;
            }
    }
    cout<<sum<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/wystan/p/4555640.html