Short toHexString

后端 未结 5 1648
别那么骄傲
别那么骄傲 2021-02-19 14:54

There are methods Integer.toHexString() and Long.toHexString(). For some reason they didn\'t implement Short.toHexString().

What

5条回答
  •  长情又很酷
    2021-02-19 15:32

    You can convert your Integer.toHexString in to a hex string for short value.

    Integer is of 32 bit, and Short is of 16 bit. So, you can just remove the 16 most significant bit from Hex String for short value converted to integer, to get a Hex String for Short.

    Integer -> -33 = 11111111 11111111 11111111 11011111  == Hex = ffffffdf
    Short   -> -33 =                   11111111 11011111  == Hex =     ffdf
    

    So, just take the last 4 characters of Hex String to get what you want.

    So, what you want is: -

    Short sh = -33;
    String intHexString = Integer.toHexString(sh.intValue());
    String shortHexString = intHexString.substring(4);
    

    I think that would work.

提交回复
热议问题