一文搞清楚Java BytesToAscii和AsciiToBytes

发布时间:2023年12月26日

BytesToAscii

 @Test
    public void convertBytesToAscii() {

        byte[] bytes = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };

        String asciiString = new String(bytes);

        System.out.println("asciiString = " + asciiString);

    }
 asciiString = 12345

在这里插入图片描述

在这里插入图片描述

AsciiToBytes

    @Test
    public void convertAsciiToBytes() {
        String asciiString = "12345";
        byte[] result = new byte[asciiString.length()];
        for (int i = 0; i < asciiString.length(); i++) {
            result[i] = (byte) asciiString.charAt(i);
        }
        System.out.println("result = " + Arrays.toString(result));
    }
result = [49, 50, 51, 52, 53]

由于 Arrays.toString()默认输出的是十进制,二前面我们输入的是十六进制,这样看起来不太顺眼。所以换一个输出:

 public static String byteArrayToHexString(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            hexString.append(String.format("%02X", b));
        }
        return hexString.toString();
    }
result = 3132333435

10进制和16进制byte对比

  public void compareBytes() {
        byte b1 = 0x31;
        byte b2 = 49;
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("compare b1 b2 = " + (b1 == b2));

        byte[] bytes1 = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
        byte[] bytes2 = new byte[] { 49, 50, 51, 52, 53 };
        System.out.println("bytes1 = " + Arrays.toString(bytes1));
        System.out.println("bytes2 = " + Arrays.toString(bytes2));
        System.out.println("compare bytes1 bytes2 = " + (bytes1 == bytes2));
    }
b1 = 49
b2 = 49
compare b1 b2 = true
bytes1 = [49, 50, 51, 52, 53]
bytes2 = [49, 50, 51, 52, 53]
compare bytes1 bytes2 = false

bytes2HexString和hexStringToBytes

   private static final byte[] HEX_ARRAY = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
   /**
     * convert byte array to hex string
     * 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 -> "313233343536"
     */
    public static String bytes2HexString(byte[] bytes) {
        byte[] hexChars = new byte[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = HEX_ARRAY[v >>> 4];
            hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
        }
        return new String(hexChars, StandardCharsets.UTF_8);
    }

    /**
     * convert byte array to hex string
     * "313233343536" ->  0x31, 0x32, 0x33, 0x34, 0x35, 0x36
     */
    public static byte[] hexStringToBytes(String hexstr) {
        int len = (hexstr.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hexstr.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (((byte) "0123456789ABCDEF".indexOf(achar[pos])) << 4
                    | (((byte) "0123456789ABCDEF".indexOf(achar[pos + 1]))) & 0xff);
        }
        return result;
    }

测试

  @Test
    public void bytes2Hex() {
        byte[] bytes = new byte[] { 0x31, 0x32, 0x33, 0x34, 0x35 };
        System.out.println("bytes = " + Arrays.toString(bytes));
        String bytes2HexString = ConvertUtils.bytes2HexString(bytes);
        System.out.println("bytes2HexString = " + bytes2HexString);

        byte[] hexStringToBytes = ConvertUtils.hexStringToBytes(bytes2HexString);
        System.out.println("hexStringToBytes = " + Arrays.toString(hexStringToBytes));
    }
bytes = [49, 50, 51, 52, 53]
bytes2HexString = 3132333435
hexStringToBytes = [49, 50, 51, 52, 53]
文章来源:https://blog.csdn.net/Shujie_L/article/details/135230986
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。