如何获得在Java中的整数0填充二进制重新presentation? [英] How to get 0-padded binary representation of an integer in java?
本文介绍了如何获得在Java中的整数0填充二进制重新presentation?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如, 1,2,128,256
输出可以是(16位):
for example, for 1, 2, 128, 256
the output can be (16 digits):
0000000000000001
0000000000000010
0000000010000000
0000000100000000
我试过
String.format("%16s", Integer.toBinaryString(1));
它把空间的左填充:
it puts spaces for left-padding:
` 1'
如何把 0
S代表填充。我找不到它在<一个href=\"http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html#syntax\">Formatter.是否有另一种方式做到这一点?
How to put 0
s for padding. I couldn't find it in Formatter. Is there another way to do it?
先谢谢了。
P.S。 这个帖子介绍如何格式化左0填充整数,但它不是二进制重新presentation。
P.S. this post describes how to format integers with left 0-padding, but it is not for the binary representation.
推荐答案
我觉得这是一个次优的解决方案,但你可以做
I think this is a suboptimal solution, but you could do
String.format("%16s", Integer.toBinaryString(1)).replace(' ', '0')
这篇关于如何获得在Java中的整数0填充二进制重新presentation?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文