16 bit hexadecimal string to int signed in Java

I have a Java string representing a signed 16 - bit value in hexadecimal The string can be passed through anything from "0000" to "ffff"

I use integer ParseInt ("ffff", 16) converts it to an integer However, this returns an unsigned value (65535)

I want it to return a signed value In this particular example, "ffff" should return - 1

How can I achieve it? Because of its 16 bit value, I want to use short Parseshort ("ffff", 16), but told me I was out of range I guess parseshort () expects a minus sign

Solution

You can set integer Convert int returned by parseint() to short:

short s = (short) Integer.parseInt("FFFF",16);
System.out.println(s);

result:

-1
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>