輸入:

byte[] a = {1, 2, 3, 15, 10, 16, 17, 32, 0, 8};

public static void main(String[] args) {
        byte[] a = {1, 2, 3, 15, 10, 16, 17, 32, 0, 8};
        System.out.println(a);//直接輸出

        System.out.println(byteArrayToHexStr(a));//轉後輸出
    }
/**
     * Byte轉16進字串工具
     */
    private static  String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        StringBuilder hex = new StringBuilder(byteArray.length * 2);
        for (byte aData : byteArray) {
            hex.append(String.format("%02X", aData));
        }
        String gethex = hex.toString();
        return gethex;

    }
}

輸出

[B@2437c6dc  //直接輸出
0102030F0A1011200008 //轉後輸出

 

直接複製部分

/**
     * Byte轉16進字串工具
     */
    private static  String byteArrayToHexStr(byte[] byteArray) {
        if (byteArray == null) {
            return null;
        }
        StringBuilder hex = new StringBuilder(byteArray.length * 2);
        for (byte aData : byteArray) {
            hex.append(String.format("%02X", aData));
        }
        String gethex = hex.toString();
        return gethex;

    }

 

arrow
arrow

    碼農日常 發表在 痞客邦 留言(1) 人氣()