我有一个包含 80 个元素的 char
数组,我正在尝试将特定元素转换为 integer
,但出现了一些数字错误。
十六进制的数组元素 40 是 0xC0。当我尝试将它分配给一个 integer
时,我得到了十六进制 0xFFFFC0,我不知道为什么。
char tempArray[80]; //Read in from file, with element 40 as 0xC0
int tempInt = (int)tempArray[40]; //Output as 0xFFFFC0 instead of 0x0000C0
最佳答案
根据您的实现,C++ 中的char
类型是 signed
类型或 unsigned
类型。 (C++ 标准要求实现选择任一方案)。
为了安全起见,请在您的情况下使用unsigned char
。
关于c++ - 将char数组中的元素转换为int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868539/