2

To keep this short, I am getting a signed number, -25771 (in Java), that I need the unsigned String representation of, which is "4294941525". Java thinks the number is signed two's complement, but I need its unsigned value.

I noticed the Javadoc for the toString() method for Integers only converts to signed representation.

Is there not a bitwise operation such as "& 0xFF" that I can do to get the unsigned number?

3 Answers 3

9

In Java 8, you can just use Integer.toUnsignedString(int i).

Sign up to request clarification or add additional context in comments.

Comments

8

I would try

int val = -25771;
System.out.println(Long.toString(val & 0xFFFFFFFFL));

prints

4294941525

or

"" + (val & 0xFFFFFFFFL)

3 Comments

Those are both giving me 0 as the output, but the solution posted earlier by aix got a value that was close (the first half of the number was correct).
Can you give an example? You should only get 0, for 0.
I figured out the problem: I was using a currentDirectory.getInt(0x011A) method to read a TIFF metadata tag and your solution works now as long as I save the int into an actual int variable. Thanks!
1

You can use Guava's UnsignedInts.toString, and parseUnsignedInt to reverse the process.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.