This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes  Integer.toHexString() on a negative value ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " Integer.toHexString() on a negative value ?" Watch " Integer.toHexString() on a negative value ?" New topic
Author

Integer.toHexString() on a negative value ?

Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

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

Regards,

Pho
Thomas Bigbee
Ranch Hand

Joined: Nov 29, 2001
Posts: 48
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
------------------
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Integer.toHexString() on a negative value ?
 
Similar Threads
Digital Signal Processing (DSP, FFT, Spectrum)
Need help to output unsigned byte > 127
Convert byte to its hex format?
how to store message digest for string in mysql database and retrieve it
Hex ??