Does BigInteger.toByteArray () return integer leading zeros?

I convert bigints to binary, radix16 and radix64 encoding and see the mysterious msb zero bursts. Is this a big problem that I can solve by removing the zero padding, or possibly doing something else?

My test code is:

String s; System.out.printf( "%s length %d\n", s = "123456789A", (new BigInteger( s, 16 )).toByteArray().length ); System.out.printf( "%s length %d\n", s = "F23456789A", (new BigInteger( s, 16 )).toByteArray().length ); 

Outputs:

  123456789A length 5 F23456789A length 6 

Of which a longer array has zero padding forward. After checking BigInteger.toByteArray () I see:

 public byte[] toByteArray() { int byteLen = bitLength()/8 + 1; byte[] byteArray = new byte[byteLen]; 

Now I can find private int bitLength; but I can't find where the Length () bit is defined to pinpoint why this class does this - possibly related to sign extension?

+2
source share
2 answers

Thanks Jon Skeet for your reply. Here is some code that I use for conversion, it is very likely that it can be optimized.

 import java.math.BigInteger; import java.util.Arrays; public class UnsignedBigInteger { public static byte[] toUnsignedByteArray(BigInteger value) { byte[] signedValue = value.toByteArray(); if(signedValue[0] != 0x00) { throw new IllegalArgumentException("value must be a psoitive BigInteger"); } return Arrays.copyOfRange(signedValue, 1, signedValue.length); } public static BigInteger fromUnsignedByteArray(byte[] value) { byte[] signedValue = new byte[value.length + 1]; System.arraycopy(value, 0, signedValue, 1, value.length); return new BigInteger(signedValue); } } 
-1
source

Yes, this is documented behavior :

The byte array will be in byte order in byte order: the most significant byte is in the zero element. The array will contain the minimum number of bytes needed to represent this BigInteger, including at least one bit of the sign , which is (ceil((this.bitLength() + 1)/8)) .

bitLength() documented as:

Returns the number of bits in the minimum representation with two additions of this BigInteger , excluding the signed bit.

In other words, two values ​​with the same value will always have the same bit length, regardless of sign. Think of BigInteger as an unsigned integer and a signed bit - and toByteArray() return all the data from both parts, which means "the number of bits required for an unsigned integer and one bit for a sign."

+10
source

Source: https://habr.com/ru/post/949757/


All Articles