Given a Hexadecimal number N, convert N into an equivalent decimal number i.e convert the number with base value 16 to base value 10. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.
Input : 1AB Output: 427 Input : 1A Output: 26
- The idea is to extract the digits of a given hexadecimal number starting from the rightmost digit.
- Keep a variable ‘dec_value’.
- At the time of extracting digits from the hexadecimal number, multiply the digit with the proper base (Power of 16) and add it to the above variable taken that is ‘dec_value’.
- In the end, the variable ‘dec_value’ will store the required decimal number.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.