I have a byte[] as an input
8B F2 99 7D 67 F1 7C 15 B2 99 2C 2E 33 DD C6 65
2D CD E7 2B
and I feed it into a BigInteger so that I can do simply compares and
easy formatting. However, when I call the toString(16) I get
74 0d 66 82 98 0e 83 ea 4d 66 d3 d1 cc 22 39 9a d2 32 18 d5
I am sure, in it's own way, it is "right" but I need to understand why
this is occurring. Since these values represent hashes I will be for
sure asked by they are the same as what was recorded for them.
Christian
http://christian.bongiorno.org
Eric Sosman - 16 Feb 2007 16:30 GMT
Sideswipe wrote On 02/16/07 09:25,:
> I have a byte[] as an input
>
[quoted text clipped - 9 lines]
> this is occurring. Since these values represent hashes I will be for
> sure asked by they are the same as what was recorded for them.
Try making the byte[] array one position longer and
inserting a zero at the beginning:
00 8B F2 99 ...
If that doesn't spark the "Aha!" moment, you could (as
a last resort) read the Javadoc for the BigInteger(byte[])
constructor, paying special attention to the words "two's
complement."

Signature
Eric.Sosman@sun.com
Sideswipe - 16 Feb 2007 20:01 GMT
AHA!
> Sideswipe wrote On 02/16/07 09:25,:
>
[quoted text clipped - 24 lines]
> --
> Eric.Sos...@sun.com
Patricia Shanahan - 16 Feb 2007 16:40 GMT
> I have a byte[] as an input
>
[quoted text clipped - 12 lines]
> Christian
> http://christian.bongiorno.org
Shouldn't there be a "-" at the start of the toString() result?
A 2's complement number, represented in hex, beginning with "8" is
negative. The output you got is the absolute value of the input. It's a
bit easier to see, at least if you can do hex arithmetic, if you line up
the input and output:
8B F2 99 7D 67 F1 7C 15 B2 99 2C 2E 33 DD C6 65 2D CD E7 2B
74 0d 66 82 98 0e 83 ea 4d 66 d3 d1 cc 22 39 9a d2 32 18 d5
Patricia
Lew - 16 Feb 2007 20:49 GMT
> Shouldn't there be a "-" at the start of the toString() result?
The value of copy and paste vs. paraphrase.
- Lew
Sideswipe - 16 Feb 2007 19:53 GMT
My thanks to the group again. I read the part about 2's compliment but
it hadn't occurred to me to simply front pad it with 00 to get what I
wanted -- that makes perfect sense.