This article explores different ways to convert a decimal number to binary in Kotlin.

A simple solution to get the binary representation of an integer in string format is using the toBinaryString() function from the Integer class.

Download Code

 
The Int.toString() function also returns the string representation of a non-negative integer in the specified radix. We can use it as:

Download Code

 
Alternately, we can use the Long.toBinaryString() function for long value.

Download Code

 
Finally, we can write custom logic to convert an integer to binary format, as shown below:

Download Code

That’s all about converting a decimal number to binary in Kotlin.