Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Question about BigIntegers and byte[] input

Thread view: 
Sideswipe - 16 Feb 2007 14:25 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.