我正在使用以下代码将 Const char *
转换为 Unsigned long int
,但输出始终为 0
。我哪里做错了?请告诉我。
这是我的代码:
#include <iostream>
#include <vector>
#include <stdlib.h>
using namespace std;
int main()
{
vector<string> tok;
tok.push_back("2");
const char *n = tok[0].c_str();
unsigned long int nc;
char *pEnd;
nc=strtoul(n,&pEnd,1);
//cout<<n<<endl;
cout<<nc<<endl; // it must output 2 !?
return 0;
}
最佳答案
使用 base-10:
nc=strtoul(n,&pEnd,10);
或允许自动检测碱基:
nc=strtoul(n,&pEnd,0);
strtoul
的第三个参数是要使用的基数,您将其设置为 base-1。
关于c++ - 将 Const char * 转换为 Unsigned long int - strtoul,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19272054/