Java : byte[] 打印 与 转16进制字符串

byte[] 转16进制字符串


public class Test {

    public static void main(String[] args) {
        String str = "你好";
        //
        System.out.println(bytesToHexFun1(str.getBytes()));
        System.out.println(bytesToHexFun2(str.getBytes()));
        System.out.println(bytesToHexFun3(str.getBytes()));
        //
        System.out.println(bytes_String16Fun1(str.getBytes()));
        System.out.println(bytes_String16Fun2(str.getBytes()));
        //
        System.out.println(bytesToHex(str.getBytes()));

    }

    private final static char[] HEX_CHAR = "0123456789ABCDEF".toCharArray();

    /**
     * byte[] 转16进制字符串
     */
    public static String bytes_String16Fun1(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (byte value : b) {
            sb.append(HEX_CHAR[value >> 4 & 0xf])
                    .append(HEX_CHAR[value & 0xf]);
        }
        return sb.toString();
    }

    public static String bytes_String16Fun2(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (byte value : b) {
            sb.append(String.format("%02x", value));
        }
        return sb.toString();
    }


    public static String bytesToHexFun1(byte[] bytes) {
        // 一个byte为8位,可用两个十六进制位标识
        char[] buf = new char[bytes.length * 2];
        int a = 0;
        int index = 0;
        for (byte b : bytes) { // 使用除与取余进行转换
            if (b < 0) {
                a = 256 + b;
            } else {
                a = b;
            }

            buf[index++] = HEX_CHAR[a / 16];
            buf[index++] = HEX_CHAR[a % 16];
        }

        return new String(buf);
    }

    public static String bytesToHexFun2(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int index = 0;
        for (byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
            buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
            buf[index++] = HEX_CHAR[b & 0xf];
        }

        return new String(buf);
    }

    public static String bytesToHexFun3(byte[] bytes) {
        StringBuilder buf = new StringBuilder(bytes.length * 2);
        for (byte b : bytes) { // 使用String的format方法进行转换
            buf.append(String.format("%02x", b & 0xff));
        }

        return buf.toString();
    }

    public static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_CHAR[v >>> 4];
            hexChars[j * 2 + 1] = HEX_CHAR[v & 0x0F];
        }
        return new String(hexChars);
    }

    public static byte[] stringToBytes(String str) {
        if (str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for (int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }

}

字节数组的打印

import java.util.Arrays;

public class Test2 {

    public static void main(String[] args) {

        //十六位数组打印输出
        Byte[] bs1 = {0x0F, 0x1F, 0x2F, 0x3F, 0x4F, 0x5F, 0x6F};
        System.out.println(Arrays.toString(bs1));

        //十六位数组打印输出
        byte[] bs2 = hexStringToByteArray("e04fd020ea3a6910a2d808002b30309d");
        System.out.println(Arrays.toString(bs2));

        //普通数组打印输出
        byte[] bs3 = "你好,这是一段字符串!".getBytes();
        System.out.println(Arrays.toString(bs3));
    }


    /**
     * 将String转换为byte[]
     *
     * @param s String
     * @return byte[]
     */
    public static byte[] hexStringToByteArray(String s) {
        int len = s.length();
        byte[] data = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) +
                    Character.digit(s.charAt(i + 1), 16));
        }
        return data;
    }

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值