Java converting int to hex and back again

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

    As Integer.toHexString(byte/integer) is not working when you are trying to convert signed bytes like UTF-16 decoded characters you have to use:

    Integer.toString(byte/integer, 16);
    

    or

    String.format("%02X", byte/integer);
    

    reverse you can use

    Integer.parseInt(hexString, 16);
    

提交回复
热议问题