将类型转换为无符号短整型 [英] type cast char to Unsigned short
本文介绍了将类型转换为无符号短整型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将字符转换为无符号短,但我得到的值m_cmdCode总是0.一些输入将是非常有帮助的。
I'm trying to convert the characters to unsigned short but the value I'm getting in m_cmdCode is always 0. Some input would be very helpful.
int main()
{
char *temp = new char[3];
memset(temp,1,3);
unsigned short m_cmdCode = (unsigned short) atoi(temp);
printf("%d",m_cmdCode);
}
//我想让m_cmdCode等于111,
// I want m_cmdCode to be equal to 111, is it possible to do this ?
推荐答案
您正在将 temp
的元素设置为整数值 1
。你想要的字符值'1'
:
You're setting the elements of temp
to the integer value 1
. You want the character value '1'
:
memset(temp, '1', 3);
注意,你还需要NUL终止 temp
atoi
可靠地工作。
Note that you also need to NUL-terminate temp
for atoi
to work reliably.
这篇关于将类型转换为无符号短整型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文