如何将带有HEX值的变量unsigned char转换为带有DEC值的变量int。我有:
unsigned char length_hex = 0x30; // HEX
如何转换为int DEC?
int length_dec = length_hex; // With result length_dec = 48
答案 0 :(得分:0)
有些人评论说转换是自动的。尝试:
int main (void)
{
unsigned char length_hex = 0x30;
printf("0x30 is char:%c and int:%d.",length_hex,length_hex);
return 0;
}
看看你得到了什么 - 当你把它转换为char时,它会' 0' 0' 0 (字符0)和它作为一个整数48。另外,如果我将常量0x30保存为int或char,它并不重要。看看:
printf("0x30 is char:%c and int:%d.",0x30,0x30);