c++ - 将 Const char * 转换为 Unsigned long int - strtoul

标签 c++ type-conversion unsigned

我正在使用以下代码将 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/

相关文章:

c++ - 使用重叠 IO 获取发件人的 IP 地址

c++ - 如何确保 lambda 的自动参数类型相同?

python - 将美式分钟字符串转换为整数值

c - 无符号溢出与 C 中的模数运算符

c++ - 如何从无符号变量写入和读取字节

c - scanf %u 负数?

c++ - 我可以使用 singleShot(true) 使正在运行的 QTimer 在下一次拍摄后停止吗?

c++ - 如何使用 OLE-DB 访问 COM/C++ ATL 应用程序中的 dbf 文件?

c++ - 如何检查整数中是否只设置了一位?

c# - 将日期和时间字符串序列化为 DateTime 对象