Java converting int to hex and back again

前端 未结 10 1155
鱼传尺愫
鱼传尺愫 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 08:57

    • int to Hex :

      Integer.toHexString(intValue);
      
    • Hex to int :

      Integer.valueOf(hexString, 16).intValue();
      

    You may also want to use long instead of int (if the value does not fit the int bounds):

    • Hex to long:

      Long.valueOf(hexString, 16).longValue()
      
    • long to Hex

      Long.toHexString(longValue)
      

提交回复
热议问题