簡體   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