Java converting int to hex and back again

前端 未结 10 1157
鱼传尺愫
鱼传尺愫 2020-11-28 08:34

I have the following code...

int Val=-32768;
String Hex=Integer.toHexString(Val);

This equates to ffff8000

int         


        
10条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 09:04

    It's worth mentioning that Java 8 has the methods Integer.parseUnsignedInt and Long.parseUnsignedLong that does what you wanted, specifically:

    Integer.parseUnsignedInt("ffff8000",16) == -32768

    The name is a bit confusing, as it parses a signed integer from a hex string, but it does the work.

提交回复
热议问题