简体   繁体   中英

Converting Signed Int to Unsigned String value in Java

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?

I would try

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

prints

4294941525

or

"" + (val & 0xFFFFFFFFL)

在Java 8中,您可以使用Integer.toUnsignedString(int i)

您可以使用Guava的UnsignedInts.toStringparseUnsignedInt来反转该过程。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM