简体   繁体   English

unsigned char hex to int dec conversion C ++

[英]unsigned char hex to int dec conversion C++

How to convert variable unsigned char with HEX value to variable int with DEC value. 如何将带有HEX值的变量unsigned char转换为带有DEC值的变量int。 I have got: 我有:

unsigned char length_hex = 0x30; // HEX

How to convert to int DEC? 如何转换为int DEC?

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

As some people have commented the conversion is automatic. 有些人评论说转换是自动的。 Try: 尝试:

int main (void)
{
    unsigned char length_hex = 0x30;
    printf("0x30 is char:%c and int:%d.",length_hex,length_hex);
    return 0;
}

and see what you get - when you convert it to a char it's '0' (the character 0) and as an int it's 48. Also, it doesn't really matter if I save the constant 0x30 as an int or char. 并看看你得到了什么 - 当你将它转换为char时它是'0'(字符0)并且作为一个int它是48.而且,如果我将常量0x30保存为int或char并不重要。 Just look at: 看看:

printf("0x30 is char:%c and int:%d.",0x30,0x30);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM