简体   繁体   中英

Get wrong result after converting byte array to String and back in Java

I got a chain of workers that work with byte array, but they have to receive and translate String and char array to the work method work(@NotNull Object data, @NotNull Object prev) as well. So I convert byte array in one worker to String nextWorker.work(new String(result, "UTF-16BE"),this); and translate it to another worker in work method where it converts back to byte array if(data instanceof String){workingData = ((String)data).getBytes("UTF-16BE");} , but result is wrong in some cases ( i pass byte arr size of N and get after byte arr size of N - 2 or something like that, and missed bytes are not from the beginning or ending of that array). Can anybody help me to solve this problem or give advise why it happens.

byte[] result = new byte[]{0, 0, 0, 97, 63, -44, 55, -19, 51, 16, -39, -106, 0, 0, 0,
                18, 0, 0, 0, 9, 0, 63, -32, 0, 0, 0, 0, 0, 0, 32, 63, -84,
                113, -57, 28, 113, -57, 28, 114, 63, -84, 113, -57, 28, 113, -57, 28,
                115, 63, -84, 113, -57, 28, 113, -57, 28, 100, 63,
                -84, 113, -57, 28, 113, -57, 28, 117, 63, -84, 113, -57, 28, 113,
                -57, 28, 108, 63, -84, 113, -57, 28, 113, -57, 28, 109, 63,
                -84, 113, -57, 28, 113, -57, 28, 111, 63, -68, 113, -57, 28, 113, -57, 28, 0};
        byte[] result2 = new byte[]{0, 0, 0, 115, 63, -29, -103, 75, 42, 114, -97, 67, 0, 0, 0, 18, 0,
                0, 0, 11, -1, 63, -84, 113, -57, 28, 113, -57, 28, 0, 63, -36, 113, -57, 28, 113, -57, 28,
                32, 63, -84, 113, -57, 28, 113, -57, 28, 112, 63, -84, 113, -57, 28, 113, -57, 28, -2, 63, -84,
                113, -57, 28, 113, -57, 28, 114, 63, -84, 113, -57, 28, 113, -57, 28, 101, 63,
                -84, 113, -57, 28, 113, -57, 28, 105, 63, -84, 113, -57, 28, 113, -57, 28, 76, 63,
                -84, 113, -57, 28, 113, -57, 28, 109, 63, -84, 113, -57, 28, 113,
                -57, 28, 111, 63, -84, 113, -57, 28, 113, -57, 28, 0};

        try {
            Object data = new String(result, "UTF-16BE");
            byte[] workingData = ((String)data).getBytes("UTF-16BE");
            Object data2 = new String(result2, "UTF-16BE");
            byte[] workingData2 = ((String)data2).getBytes("UTF-16BE");
            System.out.println( Arrays.equals(result,workingData));
            System.out.println( Arrays.equals(result2,workingData2));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

Changing encoding from UTF-16BE to UTF-16LE solves the problem in the given example.

    String data = new String(result, StandardCharsets.UTF_16LE);
    byte[] workingData = data.getBytes(StandardCharsets.UTF_16LE);
    System.out.println(Arrays.equals(result, workingData)); // true

Is there a reason for UTF-16BE encoding to preferred?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM