2013-02-14 55 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));