2013-02-14 57 views
0

当程序运行时,它在很长的时间内崩溃thislong = atoll(c);这有什么理由吗?当试图将const char *转换为long long时,程序崩溃

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; 
} 
+1

正确的缩进将是巨大的 – jogojapan 2013-02-14 05:49:32

+2

' tempBase4 [x]'返回'char'不是'const char *' – billz 2013-02-14 05:49:40

+0

'atoll'没有错误检查,为什么你会用你的方式在'char'上使用它? – chris 2013-02-14 05:50:32

回答

3

atoll需要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));