aspose file tools*
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
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
------------------
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Integer.toHexString() on a negative value ?