作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
将 ACTUAL 字符串输入到 strtuol 时出现问题。输入字符串应该是 32 位长的无符号二进制值。
显然,InputString = apple;
存在问题,但我不确定如何解决该问题。有什么想法吗?这不应该那么困难。不知道为什么我这么难过。
谢谢大家。
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
char InputString[40];
char *pEnd = NULL; // Required for strtol()
string apple = "11111111110000000000101010101000";
//cout << "Number? ";
//cin >> InputString;
InputString = apple;
unsigned long x = strtoul(InputString, &pEnd, 2); // String to long
cout << hex << x << endl;
return 1;
}
最佳答案
更好的方法是避免遗留 C 函数并使用 C++ 标准函数:
string apple = "11111111110000000000101010101000";
unsigned long long x = std::stoull(apple, NULL, 2); // defined in <string>
注意:std::stoull
实际上会在内部调用 ::strtoull
,但它允许您只处理 std::string
对象,而不必将其转换为 C 风格的字符串。
关于c++ - 如何做一个真正的strtoul?不会摄入实际的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19164549/
我是一名优秀的程序员,十分优秀!