-
String problem
I have an int variable that contains an hex number like this 084c2365. And I want to convert the first byte(65) into a decimal number (101).
Code:
char c[100];
int num; //Num has the value 084c2365
int result;
c[0]=(char)num; //c[0] gets the value from the firts byte (65)
sscanf(c[0], "%x", &result);
But I get an error at compilation the says that the 1st argument from sscanf is not an "const char *".
How can i fix this??
Thanks.
-
c[0] is a char.
initialize your variables.
-
Code:
int num = 0x084c2365;
int result = num & 0xFF;
printf("%d", result);
decimal or hexdecimal - is just representetion during output - internally - it is the same number stored as binary, no need to convert
-
Thanks, but does it matter if I have my num variable as an uint32_t and I just have the 84c2365 number without the 0x notation?
So will
Code:
int result = num & 0xFF;
make the conversion from hex to decimal??
Sorry I am a junior and I need to learn a lot.
Thanks
-
If you have 10 apples or 0x0A apples or 012 apples, how many apples do you have?
The whole idea of 'base' is just a presentation issue, so whether you say 10, 012 or 0xA doesn't matter, the underlying number is still the same.
Hex is just convenient since 2 hex digits fit exactly in one 8-bit byte, so from that point of view, it's a bit easier to imagine what is going on when you say
int result = num & 0xFF;
rather than
int result = num & 255;
But the result is exactly the same as far as the machine is concerned.
Likewise, octal was great on certain historic machines which had say 6-bits or 9-bits per byte.