Java BigInteger 到 byte[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4407779/
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
BigInteger to byte[]
提问by Kami
I need to convert a Java BigIntegerinstance to its value in bytes. From the API, I get this method toByteArray()
, that returns a byte[] containing the two's-complement representation of this BigInteger.
我需要将 Java BigInteger实例转换为其以字节为单位的值。从 API 中,我得到了这个方法toByteArray()
,它返回一个 byte[],其中包含这个 BigInteger 的二进制补码表示。
Since all my numbers are positive 128 bits (16 bytes) integer, I don't need the 2's-complement form that give me 128 bits + sign bit (129 bits)...
由于我所有的数字都是 128 位(16 字节)的正整数,因此我不需要 2 的补码形式来提供 128 位 + 符号位(129 位)...
Is there a way to get the standard (without the 2's-complement form) representation directly from a BigInteger?
有没有办法直接从 BigInteger 获得标准(没有 2 的补码形式)表示?
If not, how can I right shift the whole byte[17] array to lose the sign bit in order to get a byte[16] array?
如果没有,我怎样才能右移整个字节 [17] 数组以丢失符号位以获得字节 [16] 数组?
采纳答案by Thomas
You don't have to shift at all. The sign bit is the most significant (= leftmost) bit of your byte array. Since you know your numbers will always be positive, it is guaranteed to be 0. However, the array as a whole is right-aligned.
你根本不用换。符号位是字节数组的最重要(= 最左边)位。由于您知道您的数字始终为正数,因此它保证为 0。但是,整个数组是右对齐的。
So there are two cases: your left-most byte is 0x00 or not. If it is 0x00 you can safely drop it:
所以有两种情况:最左边的字节是否为 0x00。如果它是 0x00,你可以安全地删除它:
byte[] array = bigInteger.toByteArray();
if (array[0] == 0) {
byte[] tmp = new byte[array.length - 1];
System.arraycopy(array, 1, tmp, 0, tmp.length);
array = tmp;
}
If it is not 0, then you cannot drop it - but your array will already be in the representation you want, so you don't have to do anything.
如果它不是 0,那么你不能删除它 - 但你的数组已经在你想要的表示中,所以你不必做任何事情。
The above code should work for both cases.
上面的代码应该适用于这两种情况。
回答by Peter Lawrey
You could copy away the first byte. Or you could just ignore it.
您可以复制第一个字节。或者你可以忽略它。
BigInteger bi = BigInteger.ONE.shiftLeft(127);
byte[] bytes1 = bi.toByteArray();
System.out.println(Arrays.toString(bytes1));
byte[] bytes = new byte[bytes1.length-1];
System.arraycopy(bytes1, 1, bytes, 0, bytes.length);
System.out.println(Arrays.toString(bytes));
回答by daveb
The first (most significant) byte in the byte array may not just contain the sign bit, but normal bits too.
字节数组中的第一个(最重要的)字节可能不仅包含符号位,还包含正常位。
E.g. this BigInteger:
例如这个 BigInteger:
new BigInteger("512")
.add(new BigInteger("16"))
.add(new BigInteger("1"));
has this bit pattern: 00000010 00010001
有这个位模式:00000010 00010001
Which is to say the top byte (with the sign bit) also has 'normal' bits as you'd expect.
也就是说,顶部字节(带有符号位)也有您期望的“正常”位。
So, what do you want to get back?
那么,你想收回什么?
00000010 00010001 (what you have) or
00000100 0010001? or
10000100 01??????