Java converting int to hex and back again

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

    Using Integer.toHexString(...) is a good answer. But personally prefer to use String.format(...).

    Try this sample as a test.

    byte[] values = new byte[64];
    Arrays.fill(values, (byte)8);  //Fills array with 8 just for test
    String valuesStr = "";
    for(int i = 0; i < values.length; i++)
        valuesStr += String.format("0x%02x", values[i] & 0xff) + " ";
    valuesStr.trim();
    

提交回复
热议问题