unsigned char hex to int dec conversion C ++

时间:2016-11-09 11:16:29

标签: c++

如何将带有HEX值的变量unsigned char转换为带有DEC值的变量int。我有:

unsigned char length_hex = 0x30; // HEX

如何转换为int DEC?

int length_dec = length_hex; // With result length_dec = 48

1 个答案:

答案 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);