在 Scala 中格式化二进制值 [英] Formatting binary values in Scala

查看:41
本文介绍了在 Scala 中格式化二进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Scala 有内置的二进制数据格式化程序吗?

Does Scala have a built in formatter for binary data?

例如打印:00000011 表示 Int 值 3.

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

编写一个并不难 - 只是好奇它是否存在.

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

推荐答案

scala> 3.toBinaryString
res0: String = 11

Scala 有一个从 Int 到 RichInt 的隐式转换,它有一个 toBinaryString 方法.但是,此函数不会打印前导零.

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

这篇关于在 Scala 中格式化二进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆