How to convert a byte array to a hex string in Java?

后端 未结 27 3494
花落未央
花落未央 2020-11-21 04:19

I have a byte array filled with hex numbers and printing it the easy way is pretty pointless because there are many unprintable elements. What I need is the exact hexcode in

27条回答
  •  半阙折子戏
    2020-11-21 04:47

    // Shifting bytes is more efficient // You can use this one too

    public static String getHexString (String s) 
    {
        byte[] buf = s.getBytes();
    
        StringBuffer sb = new StringBuffer();
    
        for (byte b:buf)
        {
            sb.append(String.format("%x", b));
        }
    
    
            return sb.toString();
    }
    

提交回复
热议问题