尝试将const-char*转换为long-long时程序崩溃
Program crashes when trying to convert const char* to long long
当程序运行时,它在long-longthisLong=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));
相关文章:
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 这句话是什么意思 - " vector<long long> distance(n, std::numeric_limits<long long>::max()); "?
- C ++分割错误,为什么使用"long long"我没有得到答案?
- 以0开头的值初始化long-long会导致奇怪的值
- "1L << count"是什么意思?如何使用超出"unsigned long long int? "范围的语句编号打印?
- 为什么我的函数返回 long long 返回 705032704?
- 如何在 Turbo C++ 3.2 编译器中使用 long long 关键字
- "How to use long long data-type rather than pointers data-type to modify other variables ?"
- 只读位置'__result.std::_Rb_tree_const_iterator<_Tp>::operator*<long long int>()'分配错误
- 没有匹配函数来调用"std::basic_ofstream<char>::write(std::string*, long long unsigned int)"
- 如何处理计算中的"unsigned long long int"?
- 将long-long的数组转换为字符串
- unsigned int/signed int/long-long:无法解释的输出
- "long"和"long int"以及"long long"与"long long int"有什么区别
- map(long long int,long long int)不适用于key=1000 000 000(在long l
- 如果在我的机器上 sizeof(long) == sizeof(long long),为什么它们不是同一类型?
- long long value in Visual Studio
- C++定义了long-long,但输出是有限的
- 尝试将const-char*转换为long-long时程序崩溃