如何在java中获得整数的0填充二进制表示?
例如,对于 1, 2, 128, 256
输出可以是(16 位数字):
0000000000000001
0000000000000010
0000000010000000
0000000100000000
我尝试
String.format("%16s", Integer.toBinaryString(1));
为左侧填充添加空格:
` 1'
How to put 0
s for填充。我在 Formatter< 中找不到它/a>.还有其他方法吗?
PS这篇文章描述了如何使用左0填充格式化整数,但它不适用于二进制表示。
for example, for 1, 2, 128, 256
the output can be (16 digits):
0000000000000001
0000000000000010
0000000010000000
0000000100000000
I tried
String.format("%16s", Integer.toBinaryString(1));
it puts spaces for left-padding:
` 1'
How to put 0
s for padding. I couldn't find it in Formatter. Is there another way to do it?
P.S. this post describes how to format integers with left 0-padding, but it is not for the binary representation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
我认为这是一个次优的解决方案,但你可以这样做
I think this is a suboptimal solution, but you could do
java.util.Formatter 中没有内置二进制转换,我建议您使用 String.replace 将空格字符替换为零,如下所示:
或者实现您自己的逻辑,将整数转换为二进制表示形式,并在某处添加左侧填充沿着this中给出的路线。
或者,如果您确实需要传递数字进行格式化,您可以将二进制表示形式转换为 BigInteger,然后使用前导零对其进行格式化,但这在运行时非常昂贵,如下所示:
There is no binary conversion built into the java.util.Formatter, I would advise you to either use String.replace to replace space character with zeros, as in:
Or implement your own logic to convert integers to binary representation with added left padding somewhere along the lines given in this so.
Or if you really need to pass numbers to format, you can convert your binary representation to BigInteger and then format that with leading zeros, but this is very costly at runtime, as in:
这是旧帖子的新答案。
要将带有前导零的二进制值填充到特定长度,请尝试以下操作:
如果
len = 4
且val = 1
,则返回字符串
"10001",然后
丢弃第一个字符。所以我们得到了我们想要的:
如果
val
很可能为负,那么尝试:Here a new answer for an old post.
To pad a binary value with leading zeros to a specific length, try this:
If
len = 4
andval = 1
,returns the string
"10001"
, thendiscards the very first character. So we obtain what we want:
If
val
is likely to be negative, rather try:您可以使用 Apache Commons StringUtils 。它提供了填充字符串的方法:
You can use Apache Commons StringUtils. It offers methods for padding strings:
我尝试了以前从未真正使用过的各种方法调用来完成这项工作,它们取得了一定的成功,直到我想到了一些非常简单的方法,它可能会起作用,而且确实如此!
我确信以前已经考虑过它,不确定它对于长字符串二进制代码是否有任何好处,但对于 16 位字符串它工作得很好。希望有帮助!! (注意第二段代码已得到改进)
感谢威尔帮助改进了这个答案,使其无需循环即可工作。
这可能有点笨拙,但它有效,如果可以的话请改进并发表评论......
I was trying all sorts of method calls that I haven't really used before to make this work, they worked with moderate success, until I thought of something that is so simple it just might work, and it did!
I'm sure it's been thought of before, not sure if it's any good for long string of binary codes but it works fine for 16Bit strings. Hope it helps!! (Note second piece of code is improved)
Thanks to Will on helping improve this answer to make it work with out a loop.
This maybe a little clumsy but it works, please improve and comment back if you can....
user3608934 的想法的更简单版本“这是一个老技巧,创建一个包含 16 个 0 的字符串,然后附加您得到的修剪后的二进制字符串”:
A simpler version of user3608934's idea "This is an old trick, create a string with 16 0's then append the trimmed binary string you got ":
从 Java 11 开始,您可以使用 repeat(...) 方法:
或者,如果您需要任何整数的 32 位表示:
Starting with Java 11, you can use the repeat(...) method:
Or, if you need 32-bit representation of any integer:
我不知道“正确”的解决方案,但我可以建议您一个快速补丁。
我刚刚尝试过,发现效果很好。
I do not know "right" solution but I can suggest you a fast patch.
I have just tried it and saw that it works fine.
尝试...
我不认为这是执行此操作的“正确”方法...但它有效:)
try...
I dont think this is the "correct" way to doing this... but it works :)
我将使用如下方法编写自己的 util 类
}
输出:
相同的方法可以应用于任何整数类型。注意mask的类型
long mask = 1L <<我;
I would write my own util class with the method like below
}
Output:
The same approach could be applied to any integral types. Pay attention to the type of mask
long mask = 1L << i;
一个可行的简单解决方案是
另一种方法是
这将产生整数 5 的 16 位字符串
A naive solution that work would be
One other method would be
This will produce a 16 bit string of the integer 5
您可以使用 lib https://github.com/kssource/BitSequence。它接受一个数字并返回经过填充和/或分组的二进制字符串。
You can use lib https://github.com/kssource/BitSequence. It accept a number and return bynary string, padded and/or grouped.
// 下面将处理适当的尺寸
// Below will handle proper sizes
这是一个老技巧,创建一个包含 16 个 0 的字符串,然后附加从 String.format("%s", Integer.toBinaryString(1)) 获得的修剪后的二进制字符串,并使用最右边的 16 个字符,去掉任何前导0 的。更好的是,创建一个函数,让您指定所需的二进制字符串的长度。当然,可能还有无数其他方法可以完成此任务,包括库,但我添加这篇文章是为了帮助朋友:)
This is an old trick, create a string with 16 0's then append the trimmed binary string you got from String.format("%s", Integer.toBinaryString(1)) and use the right-most 16 characters, lopping off any leading 0's. Better yet, make a function that lets you specify how long of a binary string you want. Of course there are probably a bazillion other ways to accomplish this including libraries, but I'm adding this post to help out a friend :)
该方法将 int 转换为 String,长度=位。用 0 填充或截断最高有效位。
This method converts an int to a String, length=bits. Either padded with 0s or with the most significant bits truncated.
str[i].length()
是数字的长度,比如二进制中的 2 是 01,即长度 2将 4 更改为所需的最大数字长度。这可以优化为 O(n)。
通过使用继续。
str[i].length()
is length of number say 2 in binary is 01 which is length 2change 4 to desired max length of number. This can be optimized to O(n).
by using continue.