1. Overview
In this tutorial, we’ll show how to convert integers to and from octal and decimal representations using only the Scala standard library. Octal representation is commonly used to represent file permissions in Unix filesystems.
2. What Is the Octal Representation
The decimal representation (from now on, called integer for simplicity) is the most commonly used numeral system, based on the powers of 10. Each digit position represents a power of 10, with the rightmost digit representing units, the next representing tens, and so on.
In this representation, numbers follow the widely known sequence of 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, etc.
In the octal representation, we have a base-8 numeral system. Each digit position represents a power of 8. Octal numbers are typically represented with the prefix “0” followed by digits 0 to 7, such as o1, 07, 010, 014, etc.
In this representation, numbers follow the sequence of 00, 01, 02, 03, 04, 05, 06, 07, 010, 011, 012, 013, 014, 015, 016, 017, 020, 021, etc.
3. Converting From Integer to Octal
Converting integers to octal can be done using a few different approaches.
3.1. Using a Loop
Converting an integer to its octal representation using a loop involves repeatedly dividing the integer by 8 and collecting the remainder. The remainders, when read in reverse order, constitute the octal representation of the integer:
def decimalToOctal(decimal: Int): String = {
var result = ""
var num = decimal
while (num > 0) {
val remainder = num % 8
result = remainder.toString + result
num /= 8
}
if (result.isEmpty) "0" else result
}
And we can see the output for several cases:
scala> decimalToOctal(0)
res0: String = 0
scala> decimalToOctal(3)
res1: String = 3
scala> decimalToOctal(8)
res2: String = 10
scala> decimalToOctal(10)
res3: String = 12
3.2. Using the Int.toOctalString() Method
In Scala, you can convert an integer to its octal representation using the Int.toOctalString() method. This method directly provides the octal representation of an integer without the need for manual conversion algorithms:
scala> 0.toOctalString
res6: String = 0
scala> 3.toOctalString
res7: String = 3
scala> 8.toOctalString
res8: String = 10
scala> 10.toOctalString
res9: String = 12
3.3. Using Java’s Integer.toString()
Using the great interoperability between Scala and Java, we can use the Java method Integer.toString() to convert an integer to its octal representation by specifying the base as 8:
scala> Integer.toString(0, 8)
res10: String = 0
scala> Integer.toString(3, 8)
res11: String = 3
scala> Integer.toString(8, 8)
res12: String = 10
scala> Integer.toString(10, 8)
res13: String = 12
4. Converting From Octal to Integer
Now, let’s look at the several approaches to convert a number from its octal representation to an integer.
4.1. Using a Loop
Converting an octal integer to its decimal representation using a loop involves iterating over each digit of the octal number and multiplying it by the appropriate power of 8:
def octalToDecimal(octal: String): Int = {
var decimal = 0
var multiplier = 1
// Iterate through the octal digits in reverse order
for (i <- octal.length - 1 to 0 by -1) {
val digit = octal(i).asDigit // Convert character digit to actual digit value
decimal += digit * multiplier
multiplier *= 8 // Update multiplier for the next digit
}
decimal
}
And we can check the outcome:
scala> octalToDecimal("0")
res14: Int = 0
scala> octalToDecimal("7")
res15: Int = 7
scala> octalToDecimal("10")
res16: Int = 8
scala> octalToDecimal("12")
res17: Int = 10
4.2. Using Java’s Integer.parseInt() Method
Unfortunately, there’s no Scala native method to convert from octal to integer, but just as before, we can make use of interoperability with Java and use the Integer.parseInt() method:
scala> Integer.parseInt("0", 8)
res27: Int = 0
scala> Integer.parseInt("3", 8)
res28: Int = 3
scala> Integer.parseInt("7", 8)
res29: Int = 7
scala> Integer.parseInt("10", 8)
res30: Int = 8
scala> Integer.parseInt("12", 8)
res31: Int = 10
5. Conclusion
In this article, we learned how to convert integers between their decimal and octal representations. We started by looking at the naive approach using a loop, moved to the approach available in the Scala standard library, and finally learned how the Java interoperability capabilities could provide another solution.