Kotlin Program to Convert HexaDecimal Number to Decimal

Given a HexaDecimal number as input. Now, write a Kotlin program to convert HexaDecimal number to Decimal.

Input:
7

Output:
7
Input:
a1

Output:
161
Input:
a9

Output:
169

1. Program to Convert HexaDecimal to Decimal

A HexaDecimal number consist of either digits from 0 to 9 (i.e. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) or alphabets from ‘A’ to ‘F’ or ‘a’ to ‘f’ (i.e. ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’).
Below are the numeric value of alphabets used –

  • ‘A’ or ‘a’: 10
  • ‘B’ or ‘b’: 11
  • ‘C’ or ‘c’: 12
  • ‘D’ or ‘d’: 13
  • ‘E’ or ‘e’: 14
  • ‘F’ or ‘f’: 15

It means HexaDecimal number can contain only 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’ and ‘f’.

Note: Please note that you can also use lowercase alphabets.

Pseudo Algorithm
  • Initialise a variable decimalNum, that stores equivalent decimal value, with 0
  • Extract each digit from the hexadecimal number.
  • While extracting, multiply the extracted digit with proper base (power of 16).
  • For example, if hexadecimal number is 110, decimalNum = 1 * (16^2) + 1 * (16^1) + 0 * (16^0) = 272

Sourcecode –

fun main() {

    println("Enter n:")
    val hexaDecimalN = readLine()

    if(checkHexaDecimalNumber(hexaDecimalN!!)) {

        var i = hexaDecimalN.length - 1
        var decimalN: Long = 0
        var base = 1
        while(i >= 0) {
            val charAtPos = hexaDecimalN[i]

            val lastDigit = if((charAtPos >= '0') && (charAtPos <= '9')) {
                charAtPos - '0'
            } else if((charAtPos >= 'A') && (charAtPos <= 'F')) {
                charAtPos.toInt() - 55
            } else if((charAtPos >= 'a') && (charAtPos <= 'f')) {
                charAtPos.toInt() - 87
            } else {
                0
            }

            decimalN += lastDigit * base
            base *= 16

            i--
        }
        println("Equivalent HexaDecimal : $decimalN")
    } else {
        println("$hexaDecimalN is not a HexaDecimal number")
    }
}

private fun checkHexaDecimalNumber(hexaDecimalNum: String): Boolean {
    var isHexaDecimalNum = true

    for(charAtPos in hexaDecimalNum) {
        if(!(
                  ((charAtPos >= '0') && (charAtPos <= '9'))
               || ((charAtPos >= 'A') && (charAtPos <= 'F'))
               || ((charAtPos >= 'a') && (charAtPos <= 'f'))
        )) {
            isHexaDecimalNum = false
            break
        }
    }
    return isHexaDecimalNum
}

When you run the program, output will be –

Enter n:
abcdfe
Equivalent HexaDecimal : 11259390
Explanation:
  • We use checkHexaDecimalNumber function to check if number is hexaDecimal or not. For any number to be hexaDecimal, its’s digit should be between –
    • 0 to 9
    • A to F
    • a to f

At first, we checked if entered value is hexaDecimal or not.
If NO, we print a message “number is not hexaDecimal number”.
If Yes, we start converting those number into decimal –

  • variable base contains power of 16 to the position at which current last digit we have in lastDigit
  • Decimal value is sum of lastDigit * (power of 16 to the position at which current lastDigit is at)
  • For example,
    hexaDecimalN = 110

    decimalN = 1 * (16 ^ 2) + 1 * (16^1) + 0 * (16 ^ 0) = 272

Thus, we went through Kotlin Program to Convert HexaDecimal Number to Decimal Number.