无法访问Java中作为参数传递的部分字节数组

时间:2019-04-18 07:34:52

标签: java arrays

当我传递给我没有从中获取任何数据的方法时,我试图访问用作参数的字节数组中的数据。

下面是一个示例:

data() {
  return {
    datePickerOptions: {
          disabledDate(date) {
            // console.log(form.installation_date); // undefined form
            return date < this.form.ins_date ? this.form.ins_date : new Date();
          },
        },

}

执行后,我得到:

public class Main {

    public static void main(String[] args) {
        byte[] b = new byte[9];
        SomeOtherClass.doSomething(b, 0, b, 3, b, 6);
    }

    // Credits to StackOverFlow post for modified method (https://stackoverflow.com/a/9855338/476467)
    public static String toHexString(byte[] bytes, int offset, int length) {
        char[] hexArray = "0123456789ABCDEF".toCharArray();
        char[] hexChars = new char[length * 2];
        for (int j = offset; j < length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

}

public class SomeOtherClass {

    public static void doSomething(byte[] a, int offA, byte[] b, int offB, byte[] c, int offC) {
        // Set a
        a[offA] = (byte) 0x68;
        a[offA+1] = (byte) 0x65;
        a[offA+2] = (byte) 0x6c;

        // Set b
        b[offB] = (byte) 0x6c;
        b[offB+1] = (byte) 0x6f;
        b[offB+2] = (byte) 0x77;

        // Set c
        c[offC] = (byte) 0x6f;
        c[offC+1] = (byte) 0x72;
        c[offC+2] = (byte) 0x6c;

        // Print the byte buffers for buffer a, b, c
        System.out.println("buffer a: " + Main.toHexString(a, offA, 3));
        System.out.println("buffer b: " + Main.toHexString(b, offB, 3));
        System.out.println("buffer c: " + Main.toHexString(c, offC, 3));
    }
} 

尽管已在变量中放置了值,但不会以十六进制格式将变量打印在输出中。

我确定buffer a: buffer b: buffer c: 方法是有效的,因为我经常使用它,并且它始终有效,并且我怀疑toHexString()是否存在任何问题。

如何获取可见的值的十六进制字符串?

1 个答案:

答案 0 :(得分:1)

只需将方法toHexString中的循环更改为:

for (int j = 0; j < length; j++) {
    int v = bytes[offset + j] & 0xFF;
    hexChars[j * 2] = hexArray[v >>> 4];
    hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}

输出:

buffer a: 68656C
buffer b: 6C6F77
buffer c: 6F726C