繁体   English   中英

尝试将const char *转换为long long时程序崩溃

[英]Program crashes when trying to convert const char* to long long

当程序运行时,它很长一段时间就崩溃了thisLong = atoll(c); 有什么理由吗?

string ConvertToBaseTen(long long base4) {
    stringstream s;
    s << base4;
    string tempBase4;
    s >> tempBase4;
    s.clear();
    string tempBase10;
    long long total = 0;
    for (signed int x = 0; x < tempBase4.length(); x++) {
         const char* c = (const char*)tempBase4[x];
         long long thisLong = atoll(c);
         total += (pow(thisLong, x));
    }
    s << total;
    s >> tempBase10;
    return tempBase10;
}

环礁需要const char*作为输入,但是tempBase4[x]仅返回char

如果要将字符串中的每个字符转换为十进制,请尝试:

for (signed int x = 0; x < tempBase4.length(); x++) {
   int value = tempBase4[i] -'0';
   total += (pow(value , x));
}

或者,如果您要将整个tempBase转换为long long

long long thisLong = atoll(tempBase4.c_str());
total += (pow(thisLong, x));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM