Short toHexString

后端 未结 5 1681
别那么骄傲
别那么骄傲 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:33

    Not the simplest way to do this, but I you can get it done by converting to a byte array then converting that to a hex string.

     short a = 233;
     byte[] ret = new byte[2];
    
     ret[0] = (byte)(a & 0xff);
     ret[1] = (byte)((a >> 8) & 0xff);
    
     StringBuilder sb = new StringBuilder();
     for (byte b : ret) {
        sb.append(String.format("%02X ", b));
     }
    

    I think that will pretty much do it.

提交回复
热议问题