Hexa to Decimnal conversion in Java

Have you ever tried to print the value of int i=0xbb; ? Do you think it’ll give numeric exception? No it works well and gives you the output of ‘187’. What is this value? It’s a decimal value of ‘bb’.

Well now coming to the point if you give value to integer starts with 0x (zero alphabet x) then jvm will convert the next value after prefix (assumes it as hex value) to decimal value. In the above example the hex value is ‘bb’ and its decimal value is ‘187’ which is then stored into integer. Want to cross check ? You can also do that calculation like below.

eg1: bb -> 11 X 160 + 11 X 161 = (11+176)= 187

eg2: bd -> 13 X 160 + 11 X 161 =(13+176)=189

Confused with Decimal and Hex values? Check below.

Hex             0     1     2     3     4     5     6     7     8     9     A       B     C       D      E       F
Decimal     0     1     2     3     4     5     6     7     8     9     10     11     12     13     14     15

Note: You can see decimal values for the alphabets a to f only. If you give any alphabet other than a to f then it’ll syntax error.

Leave your comments and suggestions in below comment section. Hope you enjoyed the post 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s