C: 0x11 in Decimal
Sorry about this basic question, but why 0x11
is 17
in decimal (print(%d, 0x11)=17
?
I search information 开发者_如何学JAVAabout the way to convert from hex to dec, but it doesn't talk about this sort of numbers.
Just like "11" in base ten means "1 ten" and "1 one", "11" in base 16 (i.e. hex) means "1 sixteen" and "1 one" - or 17 in base 10.
0x
at the start of a number means that the compiler will read it as hexadecimal. 0x11
= 1 * 16 + 1 = 17
0x11 = 1*16^1 + 1*16^0 = 17. (Like 17 = 1 * 10^1 + 7 * 10^0.)
Hexadecimal counts with 16 digits, instead of 10. To account for the extra 6 digits, it uses letters A
to F
.
Counting from 0 in both base 16 and base 10:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10, 11 (base 16)
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 (base 10)
Also, to convert from base 16 to base 10:
1116 = 1 * 16 + 1 = 1710
0x11 = 1 * 16^1 (16)
+ 1 * 16^0 ( 1)
-----------------
0x11 (17)
Just like:
17 = 1 * 10^1 (10)
+ 7 * 10^0 ( 7)
-----------------
17 (17)
Well, maybe because 0x11 IS 17 in decimal?
here is an easy way of seeing
128 64 32 16 8 4 2 1 = Decimal values of the...
0 0 0 1 0 0 0 1 = Boolean switches that are ON or OFF
add them together 16 + 1 = 17
These answers are really confusing, I would suggest an easy way to understand.
Hexadecimal base is 16
0x11 is 011 (simply remove x)
For 011 we have 3 digits right?
If we start from 0th position, these digits are:
0th position(0 in 011),
1st position (1 in 011)
and 2nd position(again 1 in 011)
take positions in reverse order: 2,1,0
the calculation part
16^2 * value at first position(0th) = 16^2 * 0 = 0
16^1 * value at second position (1st) = 16^1 * 1 = 16
16^0 * value at third position (2nd) = 16^0 * 1 = 1
adding all these answers will get 0 + 16 + 1 = 17
精彩评论