美文网首页
Java 打印byte[]

Java 打印byte[]

作者: 兔宰子 | 来源:发表于2020-01-09 10:50 被阅读0次

    代码

    public static void main(String args[]) {
            System.out.println(bytesToHexString("中华人民共和国".getBytes()));
        }
        public static String bytesToHexString(byte[] src) {
            StringBuilder stringBuilder = new StringBuilder("");
            if (src == null || src.length <= 0) {
                return null;
            }
            for (int i = 0; i < src.length; i++) {
                int v = src[i] & 0xFF;
                stringBuilder.append("0x");
    
                String hv = Integer.toHexString(v);
                if (hv.length() < 2) {
                    stringBuilder.append(0);
                }
                stringBuilder.append(hv);
                if (i != src.length-1) {
                    stringBuilder.append(",");
    
                }
            }
            return stringBuilder.toString();
        }
    

    输出结果

    0xe4,0xb8,0xad,0xe5,0x8d,0x8e,0xe4,0xba,0xba,0xe6,0xb0,0x91,0xe5,0x85,0xb1,0xe5,0x92,0x8c,0xe5,0x9b,0xbd
    

    相关文章

      网友评论

          本文标题:Java 打印byte[]

          本文链接:https://www.haomeiwen.com/subject/vgcyactx.html