Formatting binary values in Scala

前端 未结 8 986
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 19:15

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

8条回答
  •  一个人的身影
    2020-12-08 19:56

    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.

提交回复
热议问题