Question about BigIntegers and byte[] input  
Author Message
Sideswipe





PostPosted: 2007-2-16 22:26:00 Top

java-programmer, Question about BigIntegers and byte[] input 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





PostPosted: 2007-2-17 0:31:00 Top

java-programmer >> Question about BigIntegers and byte[] input Sideswipe wrote On 02/16/07 09:25,:
> 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.

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."

--
email***@***.com
 
Patricia Shanahan





PostPosted: 2007-2-17 0:41:00 Top

java-programmer >> Question about BigIntegers and byte[] input Sideswipe wrote:
> 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
>

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
 
 
Sideswipe





PostPosted: 2007-2-17 3:53:00 Top

java-programmer >> Question about BigIntegers and byte[] input 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.

 
 
Sideswipe





PostPosted: 2007-2-17 4:02:00 Top

java-programmer >> Question about BigIntegers and byte[] input AHA!

On Feb 16, 11:30 am, Eric Sosman <email***@***.com> wrote:
> Sideswipe wrote On 02/16/07 09:25,:
>
> > 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.
>
> 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."
>
> --
> email***@***.com


 
 
Lew





PostPosted: 2007-2-17 4:49:00 Top

java-programmer >> Question about BigIntegers and byte[] input Patricia Shanahan wrote:
> Shouldn't there be a "-" at the start of the toString() result?

The value of copy and paste vs. paraphrase.

- Lew