Re: convert byte array to hex string using BigInteger
lipska the kat wrote:
Laura Schmidt wrote:
rossum wrote:
This is a case of RTFM. The Javadocs for BigInteger(byte[]) tell you
that it expects the byte array in two's complement representation, so
a leading 1 bit is interpreted as a negative number. You need a
different constructor:
public BigInteger(int signum, byte[] magnitude)
You are right, sorry.
Now I get a hex string without sign. But now the decode method does not
return the original bytes anymore:
private byte [] hex_decode (String val)
{
BigInteger b = new BigInteger (val,16);
byte [] t = b.toByteArray();
return (t);
}
There is no other constructor for byte arrays.
And I don't really understand why it doesn't return the original byte
array.
I'm not sure what all this twos compliment stuff is about
That's spelled "two's-complement".
twos compliment is just a number representation scheme
And the purpose of that constructor is just to convert a representation
in that scheme to the correct number.
There should be no need to 'convert' anything
If there isn't, then don't use that constructor.
--
Lew