Russell Sk. Russell Sk. - 11 months ago 168
C++ Question

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

Answer Source

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