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

时间:2016-12-12 10:38:32

标签: java arrays append concat

该计划的输出是:

[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);
    }
}

1 个答案:

答案 0 :(得分:0)

在你的结果中

[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[]?