1. Overview
Base transformations for numbers are an everyday use case when working with numbers.
In this tutorial, we’ll learn how to convert octal numbers to decimal and vice-versa in Kotlin.
2. Octal to Decimal Conversion
In this section, we’ll learn how to convert octal values to decimal numbers.
2.1. Using Integer.parseInt() Method
Let’s start by writing the octalToDecimalUsingParseInt() method that uses the parseInt() method to convert the octal representation to decimal:
fun octalToDecimalUsingParseInt(octal: String): Int {
return Integer.parseInt(octal, 8)
}
We specify the radix=8 as a parameter to parse the octal value from the base-8 number system.
Further, let’s verify the functional correctness of our function:
Assertions.assertEquals(28, octalToDecimalUsingParseInt("34"))
2.2. Using toInt() Method
Alternatively, we can use the toInt() method from the String class to parse the octal value as an integer value:
fun octalToDecimalUsingToInt(octal: String): Int {
return octal.toInt(8)
}
We call the toInt() method with a radix value of 8 to use the octal numeral system for parsing the octal string.
Now, let’s confirm that the octalToDecimalUsingToInt() method works as expected:
Assertions.assertEquals(28, octalToDecimalUsingToInt("34"))
2.3. Using String Extension Function
Let’s see how we can write the octalToDecimal() extension function for the String class:
fun String.octalToDecimal(): Int {
return Integer.parseInt(this, 8)
}
Now, let’s check that we get the desired results:
Assertions.assertEquals("34", 28.decimalToOctal())
That’s it! This approach adds convenience along with the core functionality.
2.4. Using BigInteger
When we have large values for conversion, we can use the BigInteger class.
Let’s first create a BigInteger value from the octal string and then get a decimal value as a result:
fun octalToDecimalUsingBigInteger(octal: String): Int {
val bigInt = BigInteger(octal, 8)
return bigInt.toInt()
}
It’s always recommended to verify our approach with a simple test:
Assertions.assertEquals(28, octalToDecimalUsingBigInteger("34"))
It looks good!
2.5. Using Iteration
Let’s see how we can iterate over each character of the octal value and compute the equivalent decimal value:
fun octalToDecimalUsingIteration(octal: String): Int {
var decimal = 0
var power = 0
for (position in octal.length - 1 downTo 0) {
val digit = octal[position].toString().toInt()
decimal += digit * Math.pow(8.0, power.toDouble()).toInt()
power++
}
return decimal
}
It’s important to note that we parse the string from right to left and calculate its place value as digit*8^position. Further, we add this to the resultant decimal value in each iteration.
Additionally, let’s verify that our method is working correctly:
Assertions.assertEquals(28, octalToDecimalUsingIteration("34"))
3. Decimal to Octal Conversion
In this section, let’s learn how to convert decimal to octal values.
3.1. Using Integer.toString() Method
Let’s start by writing the decimalToOctalUsingIntegerToString() method for converting decimal to octal value:
fun decimalToOctalUsingIntegerToString(decimal: Int): String {
return Integer.toString(decimal, 8)
}
We use the toString() method internally and pass the radix value as 8.
Additionally, let’s check the outcome for a sample value:
Assertions.assertEquals("34", decimalToOctalUsingIntegerToString(28))
Great! It looks like we’ve got this one right.
3.2. Using String Interpolation
Alternatively, we can use string interpolation with the %0 format string to convert decimal value to a base-8 string:
fun decimalToOctalUsingStringInterpolation(decimal: Int): String {
return "%o".format(decimal)
}
Now, let’s confirm that we get the correct result with our approach:
Assertions.assertEquals("34", decimalToOctalUsingStringInterpolation(28))
3.3. Using Int Extension Function
We can also extend the Int class with the decimalToOctal() extension function:
fun Int.decimalToOctal(): String {
return Integer.toString(this, 8)
}
Next, let’s call the decimalToOctal() function for an Int value:
Assertions.assertEquals("34", 28.decimalToOctal())
3.4. Using Iteration
Lastly, let’s see how we can use iteration to convert a decimal value to its octal representation:
fun decimalToOctalUsingIteration(decimal: Int): String {
var octal = ""
var num = decimal
while (num > 0) {
octal = (num % 8).toString() + octal
num /= 8
}
return octal
}
We repeatedly divide the original decimal value by 8 and note the remainder in the reverse order.
Furthermore, let’s verify the result works as expected:
Assertions.assertEquals("34", decimalToOctalUsingIteration(28))
4. Conclusion
In this article, we learned multiple techniques for converting decimal values to octal values and vice-versa.
As always, the code from this article is available over on GitHub.