Explanation Program 1:
065 is octal its Decimal value is 53.
0x65 and 0X65 in decimal is 114.
Program 2:
octal of 65 is 101.
hexadecimal of 65 is 41.
For more Details read the following Concept:
0x or 0X means Hexadecimal numbers, the range is 0-15 unlike decimal numbers whose range is 0-9.
Hexadecimal digits are: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f
If we write 0xf + 1 , the value that we will get is 10.
printf("%x", 0xf+1); // %x is used to print a number in hexadecimal format.
prints "10"
this number 10 in binary is represented as : 0001 0000.
Here f was represented as 1000.
adding 1 to is generates a carry of 1. so 1 0000 .
We can add 3 zeros before 1 to represent it as 4 bits.
which can be written as 0001 0000.
Converting each group in to decimal we get 1 0 EHnce the output is 10.
Here comes the main concept:
If you see a hexadecimal number try to represent each digit in binary representation of length 4 bits only.
So 72 is represented as : 0111 0010
Now remove the space and write them together as 01110010.
Convert it into Decimal by following the following steps:
Remember the rightmost bit is 0th bit, the second bit from right is the 1st bit.. and so on..
from the right hand side the 0th bit is 0 and 1st,4th,5th,6th bits are 1.
Evaluating it:
2^1 + 2^4 + 2^5 + 2^6 = 2 + 16 + 32 + 64 = 114.
Thus 0x72 or 0X72 becomes 114 in decimal format!!
Like wise If you wanted to convert a decimal in to Hexadecimal, start by forming groups of 4 bits from the right hand side.
So 172 in Decimal is : 10101100,
creating group of 4 bits we get: 1010 1100.
Convert the 2 groups into Decimal again: 10 12
Now 10 is represented as "a" in hexadecimal and 12 is represented as "c".
Hence the Hexadecimal Equivalent is "ac".
This should have presented a better picture of Hexadecimal Concepts.
Now lets move on to Octal values.
Octal numbers are a little confusing in tha way it is written in c.
It starts with a "0" (zero).
The octal numbers have a range of 0-7, because it deals with just 3 bits unlike hexadecimals which deals with 4 bits.
Octal numbers are:0,1,2,3,4,5,6,7.
printf("%o", 07+1); // %x is used to print a number in hexadecimal format.
prints "10" as output.
7 is represented as 111 in Binary and if we add 1 we get 000 with a carry of 1 i.e. 1 000.
Which can be written as 001 000, Converting each group in to decimal we get: 10.
Now similarly, It you get any decimal number like : 123,
step 1: Represent it in binary -> 123 = 01111011
step 2: Split it into groups of 3 bits from right hand side and add padding of 0s on the left hand side if the number of bits is not a multiple of 3.
we get: 001 111 011
step 3: Convert each group in to decimal : 1 7 3.
Hence the octal representation of 123 is 173.
Hope this makes you good at handling octal and Hexadecimal values.