Search code examples
scalastring-formatting

Formatting binary values in Scala


Does Scala have a built in formatter for binary data?

For example to print out: 00000011 for the Int value 3.

Writing one won't be difficult - just curious if it exists.


Solution

  • scala> 3.toBinaryString
    res0: String = 11
    

    Scala has an implicit conversion from Int to RichInt which has a method toBinaryString. This function does not print the leading zeroes though.