Carramba said:
hi!
int digit = 12;
printf("%x\n", digit);
running this code I get 'c' as output? that is the problem?
%x is a conversion specifier that makes any of the printf
family of functions to print numbers in hexadecimal.
---
§7.19.6.1#8
o,u,x,X
The unsigned int argument is converted to unsigned octal (o), unsigned
decimal (u), or unsigned hexadecimal notation (x or X) in the style
dddd; the letters abcdef are used for x conversion and the letters
ABCDEF for X conversion. The precision specifies the minimum number of
digits to appear; if the value being converted can be represented in
fewer digits, it is expanded with leading zeros. The default precision
is 1. The result of converting a zero value with a precision of zero is
no characters.
---
<ot>
Decimal Hexadecimal Binary
-------------------------------
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
10 A 1010
11 B 1011
12 C 1100
13 D 1101
14 E 1110
15 F 1111
Visualize an odometer when reading the above table.
(An odometer records the distance that a vehicle travels.)
Read up on hexadecimal and learn to count in
these number systems.
Decimal -- base-10
Hexadecimal -- base-16
Binary -- base-2
All this means is that decimal has 10 digits
that one can use to represent numbers. Binary uses 2 digits.
Hexadecimal uses 16 digits to represent numbers (0 through F).
Yes, A, B, C, D, E, and F are treated as digits in hex.
The last digit in decimal is 9, that in binary is 1, and
that in hexadecimal is F. "10" in any number system
always comes after the last digit. This means
decimal 10 is not the same as hex 10 or binary 10.
Remember never to mix number systems in operations.
Suggested reading:
http://mathworld.wolfram.com/Base.html
http://mathworld.wolfram.com/Hexadecimal.html
http://mathworld.wolfram.com/Binary.html
</ot>
Regards,
Jonathan.