如何做真正的斯特图尔?不会引入实际字符串
How to do a true strtoul? Wont intake an actual string
在
向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;
}
更好的方法是避免使用legacy-C函数并使用C++标准函数:
string apple = "11111111110000000000101010101000";
unsigned long long x = std::stoull(apple, NULL, 2); // defined in <string>
注意:std::stoull
实际上会在内部调用::strtoull
,但它允许您只处理std::string
对象,而不必将其转换为 C 样式字符串。
包括:
#include<cstdlib> // for strtol()
#include<cstring> // for strncpy()
然后
strncpy(InputString ,apple.c_str(),40);
^
|
convert to C ctring
或者简单地说,
unsigned long x = strtoul(apple.c_str(), &pEnd, 2);
相关文章:
- 我正在尝试使用 while 循环从字符串中删除字母,直到没有字母。我在这里做错了什么?
- 我用字符串做了一个C++构造函数,但它不会打印出字符串
- 我做错了什么?反向字符串 C++
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 当正斜杠运算符应用于C++中的字符串类型时,它会做什么
- 我在查找和字符串上做错了什么?
- std::字符串 's' 通过 '&s[0]' 转换为字符* - 如何/为什么这样做?
- 有没有办法让这些变量从真切换到假,还是我做错了
- C++子字符串为什么这样做
- 字符 * 和字符串在C++中有何不同?(在描述中编写代码)
- 无法获得字符串:: size_type int做touper
- 从一个字符串中获取 2-5 个连续的单词短语,我得到了 2 个工作,但在做 3 个时遇到麻烦
- C ++在比较字符串时忽略前空格,例如:"str1"比较。( " str2" ) = 真
- 如何做字符串反转,以C++或C计算长度
- 如何正确地将std::字符串转换为无符号char[]数组*.我觉得我做错了,有人给我指明了正确的方向
- C++中的字符串分配:为什么这样做
- 做..while() 将最后一个字符串重复两次
- 如何做字符串::复制C++的反义词
- 如何做真正的斯特图尔?不会引入实际字符串
- 什么是操作员!!在c++的字符串上做