Question about BigIntegers and byte[] input

S

Sideswipe

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
 
E

Eric Sosman

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

Patricia Shanahan

Sideswipe said:
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
 
S

Sideswipe

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

Sideswipe

AHA!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top