gpt4 book ai didi

c++ - INTtoCHAR 函数计算出错误的值

转载 作者:行者123 更新时间:2023-11-28 00:15:36 25 4
gpt4 key购买 nike

这是我的函数的样子:

signed char INTtoCHAR(int INT)
{
signed char CHAR = (signed char)INT;
return CHAR;
}

int CHARtoINT(signed char CHAR)
{
int INT = (int)CHAR;
return INT;
}

它将 int 值分配给 char 时工作正常,但是当我想计算那个 char 时,它会给我一些奇怪的信号。它编译没有错误。

我的测试代码是:

int main()
{
int x = 5;
signed char after;
char compare = '5';
after = INTtoCHAR(5);

if(after == 5)
{
std::cout << "after:" << after << "/ compare: " << compare << std::endl;
}


return 0;
}

After 确实是 5 但它不打印 5。有什么想法吗?

最佳答案

除了使用一元运算符 + 的上述答案外,还有另一种方法:typecasting

std::cout << "after:" << (int)after << "/ compare: " << compare << std::endl;

Correct output

关于c++ - INTtoCHAR 函数计算出错误的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30546308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com