简体   繁体   中英

unsigned char hex to int dec conversion C++

How to convert variable unsigned char with HEX value to variable int with DEC value. I have got:

unsigned char length_hex = 0x30; // HEX

How to convert to 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. Just look at:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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