java 如何将 long 转换为固定长度的 16 位二进制字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2621017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:03:32  来源:igfitidea点击:

How to convert a long to a fixed-length 16-bit binary string?

java

提问by uckelman

Hi i want to convert a long integer to binary but the problem is i want a fixed 16 bit binary result after conversion like if i convert 2 to 16 bit binary it should give me 0000000000000010 as ans can anyone help me ?

嗨,我想将一个长整数转换为二进制,但问题是转换后我想要一个固定的 16 位二进制结果,就像我将 2 位转换为 16 位二进制一样,它应该给我 0000000000000010,因为有人可以帮助我吗?

回答by uckelman

Most likely what you want is Integer.toBinaryString(), combined with something to ensure that you get exactly 16 places:

很可能你想要的是Integer.toBinaryString(),结合一些东西来确保你得到正好 16 个位置:

int val = 2;
String bin = Integer.toBinaryString(0x10000 | val).substring(1);

The idea here is to get the zero padding by putting a 1 in the 17th place of your value, and then use String.substring()to chop off the leading 1 this creates, thus always giving you exactly 16 binary digits. (This works, of course, only when you are certain that the input is a 16-bit number.)

这里的想法是通过在您的值的第 17 位放置一个 1 来获得零填充,然后使用它String.substring()来砍掉由此创建的前导 1,从而始终为您提供 16 个二进制数字。(当然,这仅在您确定输入是 16 位数字时才有效。)

回答by malaverdiere

I'm presuming that you want a String output of fixed length (16). Here's what the code would look like:

我假设您想要一个固定长度 (16) 的字符串输出。代码如下所示:

String binarized = Integer.toBinaryString(i);
int len = binarized.length();
String sixteenZeroes = "00000000000000000";
if (len < 16)
  binarized = sixteenZeroes.subString(0, 16-len).concat(binarized);
else
  binarized = binarized.subString(len - 16);
return binarized;

Warning: I didn't compile or run it, so make sure no bug is there :)

警告:我没有编译或运行它,所以请确保没有错误 :)

回答by Thirler

In contrast to many suggestions here: Integer.toBinaryString, doesn't work for a 16 bit (a short) and it will not print leading zero's. The reason is that (as the name suggests) this will only work for integers. And for negative numbers the bit representation will change (the first bit indicates a negative number). The two numbers below represent the same number in short and int. So if you want to represent the raw bits you have received (this is the general application of your problem), this function will generate strange output.

与这里的许多建议相反:Integer.toBinaryString, 不适用于 16 位(短)并且不会打印前导零。原因是(顾名思义)这仅适用于整数。对于负数,位表示将改变(第一位表示负数)。下面的两个数字在 short 和 int 中代表相同的数字。所以如果你想表示你收到的原始位(这是你问题的一般应用),这个函数会产生奇怪的输出。

decimal: -3
short:                     1111 1111 1111 1101
int:   1111 1111 1111 1111 1111 1111 1111 1101

EDIT: Changed the number above

编辑:更改了上面的数字

Hence you can not cast the short if you are interested in the bit.

因此,如果您对该位感兴趣,则无法投出短篇。

Java doesn't provide the implementation for short, so you will have to provide your own. Something like this (size is the number of bits):

Java 不提供简短的实现,因此您必须提供自己的实现。像这样(大小是位数):

int displayMask = 1 << (size - 1);
StringBuffer buf = new StringBuffer( size);
for ( int c = 1; c <= size; c++ ) 
{
    buf.append( ( value & displayMask ) == 0 ? '0' : '1' );
    value <<= 1;
}

回答by Shekhar

I had to do it for a 32 bit number and ended up with:

我不得不为 32 位数字做这件事,结果是:

String stringWord = Long.toBinaryString(word);
while (stringWord.length() < 32) // ensure that length of word is 32
        stringWord = "0" + stringWord;

回答by polygenelubricants

Integer.toBinaryStringwill convert an intto its binary representation as a string.

Integer.toBinaryString将 an 转换int为字符串形式的二进制表示。

It does not give you leading zeroes, so if you really need the string to have those and be 16 bits, you can just add them yourself.

它不会给你前导零,所以如果你真的需要字符串有这些并且是 16 位,你可以自己添加它们。

You should know how to do that.

你应该知道怎么做。



Do note that an intis actually 32 bits in Java. You should also know how two's complement works. The binary representation of -1, for example, is 32 1s.

请注意,int在 Java中 an实际上是 32 位。您还应该知道二进制补码的工作原理。-1例如,的二进制表示是 321秒。

回答by dckrooney

In terms of an algorithm to convert base10 numbers to binary, I personally think the following is pretty straightforward:

就将 base10 数字转换为二进制的算法而言,我个人认为以下内容非常简单:

char[] array;

for (i; i < 16; i++)
{
    if (yourNumber % 2 == 0)
          array[16-i] = '0';
    else if (yourNumber % 2 == 1)
          array[16-i] = '1';
    yourNumber = yourNumber / 2;
}

You can then convert your char array to a String if you like.

然后,您可以根据需要将字符数组转换为字符串。

回答by PATRY Guillaume

if you want the binary representation of a long, then there is a method in the Long objet to do so :

如果你想要一个 long 的二进制表示,那么 Long 对象中有一个方法可以这样做:

String Long.toString(long i, int radix);

with a radix of 2, you should have a binary representation.

基数为 2,你应该有一个二进制表示。

regards
Guillaume

问候
纪尧姆

回答by kgiannakakis

Binary is a representation and not a format to convert an integer to. For example, if you have an integer:

二进制是一种表示形式,而不是将整数转换为的格式。例如,如果您有一个整数:

int i = 2;

The binary representation will be 00000010. Java has only signed integers, so this linkwill be helpful.

二进制表示将是 00000010。Java 只有带符号的整数,所以这个链接会很有帮助。