1. Overview

In this tutorial, we’ll see how to convert a number into its binary representation in Scala using the several methods offered by the Scala standard library.

2. What Is Binary Representation?

Everything has a binary representation in the digital world. Every letter or number has a binary representation which consists of a sequence of 0 and 1 characters. A binary number is a number expressed in the base-2 numeral system or binary numeral system.

Let’s see how the first few integers can be represented in binary:

0 -> 0000
1 -> 0001
2 -> 0010
3 -> 0011
4 -> 0100
5 -> 0101
6 -> 0110
7 -> 0111
8 -> 1000

Notably, the leading zeros can be omitted.

3. Using a Recursive Approach

The first possible approach to get the binary representation of a number is by using a recursive approach:

import scala.collection.immutable._

def toBinary(n: Int): List[Int] = {
  def innerBinary(acc: List[Int], n: Int): List[Int] = {
    n match {
      case 0 | 1 => n :: acc
      case _ => innerBinary((n % 2) :: acc, (n / 2))
    }
  }
  innerBinary(List.empty,n)
}

The main idea here is that to convert into a binary number, we need to divide the number by 2 recursively and then we use the remainder of the division as well.

Let’s see our recursive method in action:

scala> toBinary(0)
res0: List[Int] = List(0)

scala> toBinary(1)
res1: List[Int] = List(1)

scala> toBinary(4)
res2: List[Int] = List(1, 0, 0)

scala> toBinary(5)
res3: List[Int] = List(1, 0, 1)

The proposed solution returns a List of numbers. We can easily convert it into a full String by using the List.mkString() method at the end:

scala> def toBinary(n: Int): String = {
     |   def innerBinary(acc: List[Int], n: Int): List[Int] = {
     |     n match {
     |       case 0 | 1 => n :: acc
     |       case _ => innerBinary((n % 2) :: acc, (n / 2))
     |     }
     |   }
     |   innerBinary(List.empty,n).mkString
     | }
toBinary: (n: Int)String

scala> toBinary(10)
res0: String = 1010

One small improvement we can make is using the @tailrec annotation to tell the compiler to verify the generated code is using tail recursion. This ensures we won’t have any stack overflow error.

4. Using Java Interoperability

One of Scala’s strongest selling points is its interoperability with Java. This allows Scala code to call any Java existing code. Fortunately, Java offers a function to convert Integer into any base other than decimal using Integer.toString():

scala> Integer.toString(0,2)
res13: String = 0

scala> Integer.toString(1,2)
res15: String = 1

scala> Integer.toString(4,2)
res16: String = 100

scala> Integer.toString(5,2)
res17: String = 101

This solution doesn’t print the leading zeroes.

5. Using Scala Conversion

Finally, Scala also offers a standard lib solution using the Int.toBinaryString() method:

scala> 0.toBinaryString
res21: String = 0

scala> 1.toBinaryString
res22: String = 1

scala> 4.toBinaryString
res24: String = 100

scala> 5.toBinaryString
res25: String = 101

This approach is very similar to the Java counterpart we saw in the previous section.

6. Conclusion

In this article, we saw how to convert an integer number into its binary representation in Scala. We saw three different approaches, starting from the naive approach where we created a recursive function to manually convert the number and then moved to use the standard lib methods offered both in Java and in Scala.