Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;从字符串输入将二进制转换为十进制_C++_Binary_Decimal - Fatal编程技术网

C++ C++;从字符串输入将二进制转换为十进制

C++ C++;从字符串输入将二进制转换为十进制,c++,binary,decimal,C++,Binary,Decimal,我有一个问题,因为我有字符串输入,我想把它转换成十进制 这是我的密码: #include <iostream> #include <string> #include <stdlib.h> using namespace std; string inputChecker; int penghitung =0; int main(){ string source = "10010101001011110101010001"; cout &l

我有一个问题,因为我有字符串输入,我想把它转换成十进制

这是我的密码:

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

using namespace std;

string inputChecker;
int penghitung =0;

int main(){
    string source = "10010101001011110101010001";

    cout <<"Program Brute Force \n";
    cout << "Masukkan inputan : ";
    cin >> inputChecker;

    int pos =inputChecker.size();
    for (int i=0;i<source.size();i++){
        if (source.substr(i,pos)==inputChecker){
            penghitung +=1;
        }
    }
    if (source.find(inputChecker) != string::npos)
        cout <<"\nData " << inputChecker << " ada pada source\n";
    else
        cout <<"\nData "<< inputChecker <<" tidak ada pada source\n";

    cout <<"\nTotal kombinasi yang ada pada source data adalah  " <<penghitung <<"\n";
    cout <<"\nDetected karakter adalah " <<inputChecker;
    cout <<"\nThe Decimal is :" <<inputChecker;
}
#包括
#包括
#包括
使用名称空间std;
字符串输入检测器;
int-penghitung=0;
int main(){
字符串source=“10010101011110101010001”;
不能以2为基数。例如

auto result = std::strtol(source.c_str(), nullptr, 2);
以2为基数使用。例如

auto result = std::strtol(source.c_str(), nullptr, 2);
对于暴力:

static const std::string text_value("10010101001011110101010001");
const unsigned int length = text_value.length();
unsigned long numeric_value = 0;
for (unsigned int i = 0; i < length; ++i)
{
  value <<= 1;
  value |= text_value[i] - '0';
}
static const std::string text_值(“10010101001011110101010001”);
常量unsigned int length=text_value.length();
无符号长数值_值=0;
for(无符号整数i=0;i
static const std::string text_value("10010101001011110101010001");
const unsigned int length = text_value.length();
unsigned long numeric_value = 0;
for (unsigned int i = 0; i < length; ++i)
{
  value <<= 1;
  value |= text_value[i] - '0';
}
static const std::string text_值(“10010101001011110101010001”);
常量unsigned int length=text_value.length();
无符号长数值_值=0;
for(无符号整数i=0;i值,它说结果没有命名类型,那你就不用C++11了。你可以用
long
来代替
auto
。现在它说nullptr没有声明。我应该使用什么?用0代替
nullptr
。结果不是很好lol。它说结果是39107921,即使我输入了101个二进制-hhahaesnt命名一个类型SIR那么你没有使用C++11。你可以使用
long
代替
auto
。现在它说nullptr没有声明。我应该使用什么?只需使用0而不是
nullptr
。结果不是很好的lol。即使我输入101个二进制-\uuuh-hhaha,结果也是39107921。使用
std::bitset
。这篇文章可以帮助你:使用
std::bitset
。这篇文章可以帮助你: