在 C++ 中将二进制数转换为十进制数
Translation from binary into decimal numbers in C++
我尝试构建一个函数,将存储在字符串中的二进制数计算为存储在 long long
中的十进制数。我认为我的代码应该可以工作,但没有。
在此示例中,二进制数 101110111
的十进制数为 375
。但是我的输出完全令人困惑。
这是我的代码:
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
int main() {
std::string stringNumber = "101110111";
const char *array = stringNumber.c_str();
int subtrahend = 1;
int potency = 0;
long long result = 0;
for(int i = 0; i < strlen(array); i++) {
result += pow(array[strlen(array) - subtrahend] * 2, potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
}
这是输出:
1
99
9703
894439
93131255
9132339223
894974720087
76039722530902
8583669948348758
我哪里做错了?
您忘记将数字转换为整数。另外,您真的不需要使用 C 字符串。
这是代码的更好版本
int main() {
std::string stringNumber = "101110111";
int subtrahend = 1;
int potency = 0;
long long result = 0;
for(int i = 0; i < stringNumber.size(); i++) {
result += pow(2*(stringNumber[stringNumber.size() - subtrahend] - '0'), potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
}
从字符串数字中减去 '0'
会将数字转换为整数。
现在为了加分写一个不使用 pow
的版本(提示:potency *= 2;
而不是 potency++;
)
'1' != 1
正如@churill 在评论中提到的。 '1' == 49
。如果您在 linux 上,请在终端中键入 man ascii
以获取 ascii table.
试试这个,是一样的代码。我只是直接使用 stringNumber
而不是使用 const char*
。我从当前索引中减去 '0'
。 '0' == 48
,所以如果你减去它,你会得到实际的 1
或 0
整数值:
auto sz = stringNumber.size();
for(int i = 0; i < sz; i++) {
result += pow((stringNumber[sz - subtrahend] - '0') * 2, potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
此外,像.size()
一样使用std::string
提供的方法,而不是在每次迭代时都使用strlen()
。快多了。
在生产环境中,我强烈建议使用 std::bitset
而不是推出您自己的解决方案:
std::string stringNumber = "1111";
std::bitset<64> bits(stringNumber);
bits.to_ulong();
c++ 方式
#include <string>
#include <math.h>
#include <iostream>
using namespace std;
int main() {
std::string stringNumber = "101110111";
long long result = 0;
uint string_length = stringNumber.length();
for(int i = 0; i <string_length; i++) {
if(stringNumber[i]=='1')
{
long pose_value = pow(2, string_length-1-i);
result += pose_value;
}
}
std::cout << result << std::endl;
}
我尝试构建一个函数,将存储在字符串中的二进制数计算为存储在 long long
中的十进制数。我认为我的代码应该可以工作,但没有。
在此示例中,二进制数 101110111
的十进制数为 375
。但是我的输出完全令人困惑。
这是我的代码:
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <string.h>
int main() {
std::string stringNumber = "101110111";
const char *array = stringNumber.c_str();
int subtrahend = 1;
int potency = 0;
long long result = 0;
for(int i = 0; i < strlen(array); i++) {
result += pow(array[strlen(array) - subtrahend] * 2, potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
}
这是输出:
1
99
9703
894439
93131255
9132339223
894974720087
76039722530902
8583669948348758
我哪里做错了?
您忘记将数字转换为整数。另外,您真的不需要使用 C 字符串。
这是代码的更好版本
int main() {
std::string stringNumber = "101110111";
int subtrahend = 1;
int potency = 0;
long long result = 0;
for(int i = 0; i < stringNumber.size(); i++) {
result += pow(2*(stringNumber[stringNumber.size() - subtrahend] - '0'), potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
}
从字符串数字中减去 '0'
会将数字转换为整数。
现在为了加分写一个不使用 pow
的版本(提示:potency *= 2;
而不是 potency++;
)
'1' != 1
正如@churill 在评论中提到的。 '1' == 49
。如果您在 linux 上,请在终端中键入 man ascii
以获取 ascii table.
试试这个,是一样的代码。我只是直接使用 stringNumber
而不是使用 const char*
。我从当前索引中减去 '0'
。 '0' == 48
,所以如果你减去它,你会得到实际的 1
或 0
整数值:
auto sz = stringNumber.size();
for(int i = 0; i < sz; i++) {
result += pow((stringNumber[sz - subtrahend] - '0') * 2, potency);
subtrahend++;
potency++;
std::cout << result << std::endl;
}
此外,像.size()
一样使用std::string
提供的方法,而不是在每次迭代时都使用strlen()
。快多了。
在生产环境中,我强烈建议使用 std::bitset
而不是推出您自己的解决方案:
std::string stringNumber = "1111";
std::bitset<64> bits(stringNumber);
bits.to_ulong();
c++ 方式
#include <string>
#include <math.h>
#include <iostream>
using namespace std;
int main() {
std::string stringNumber = "101110111";
long long result = 0;
uint string_length = stringNumber.length();
for(int i = 0; i <string_length; i++) {
if(stringNumber[i]=='1')
{
long pose_value = pow(2, string_length-1-i);
result += pose_value;
}
}
std::cout << result << std::endl;
}