如何获取两个连接字符串的字节值...?

How to get byte values of two concatenated strings...?

程序的输出是:

[B@171ccb0[B@35378d
[B@1d23632

要求的输出是:

[B@171ccb0[B@35378d
[B@171ccb0[B@35378d

请帮忙...

import java.util.Scanner;
import java.io.InputStreamReader;

public class testme {
    public static void main(String[] args) {
        Scanner in = new Scanner(new InputStreamReader(System.in));
        String s = "hello";
        String sb = "hi";
        String sc = s.concat(sb);
        byte[] a, b;
        a = s.getBytes();
        b = sb.getBytes();
        byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);
        System.out.println(a + "" + b + "\n" + c);
    }
}

在你的结果中

[B@171ccb0, the [B means byte[] and 171ccb0 is the memory address of a variable used, separated by an @.

Similarly, in [B@35378d , 35378d is the memory address of b variable created, and

in [B@1d23632, 1d23632 is the memory address of c variable created.

如果您想知道值是否串联,请尝试打印 Arrays.toString 方法,

import java.util.Arrays;

public class Solution {
    public static void main(String[] args) {
        String s = "hello";
        String sb = "hi";
        byte[] a, b;
        a = s.getBytes();
        b = sb.getBytes();

        byte[] c = new byte[a.length + b.length];
        System.arraycopy(a, 0, c, 0, a.length);
        System.arraycopy(b, 0, c, a.length, b.length);

        System.out.println(" Arrays.toString(a)= "+Arrays.toString(a));
        System.out.println(" Arrays.toString(b)= "+Arrays.toString(b));
        System.out.println(" Arrays.toString(c)= "+Arrays.toString(c));

    }
}

输出为:

Arrays.toString(a)= [104, 101, 108, 108, 111] 
Arrays.toString(b)= [104, 105] 
Arrays.toString(c)= [104, 101, 108, 108, 111, 104, 105]

参考:How to convert Java String into byte[]?