Formatting binary values in Scala

前端 未结 8 973
伪装坚强ぢ
伪装坚强ぢ 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:54

    I usually use to prepend zeroes of the wanted length -1 and then just chop the rightmost characters:

    "0000000" + 3.toBinaryString takeRight 8
    

    This works fine for negative values as well.

提交回复
热议问题