This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integer.toHexString() on a negative value ?

 
Pho Tek
Ranch Hand
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a program that generated a 20 byte SHA-1 hash value.
The values are in a byte array and I am trying to print them
out as hexadecimal string.
I am having problems printing out negative values.
e.g. byte b = (byte)0xff;
System.out.println( Integer.toHexString(b) );
produces fffffffd ?? Shouldn't I get back ff ?
Thanks
Pho
 
Thomas Bigbee
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe the below from the api states exactly what is happening
toHexString
Creates a string representation of the integer argument as an unsigned integer in base 16.
The unsigned integer value is the argument plus 2^32 if the argument is negative; otherwise, it is equal to the argument.
Hope that helps, Tom
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic