Java:“[B@1ef9157”背后的语法和含义? 二进制/地址?

发布于 2024-07-26 04:45:33 字数 697 浏览 2 评论 0原文

嘿,我正在尝试弄清楚 [B@ 前缀在 java 中的含义。当我尝试打印字节数组时,它们就会出现。 但是,大小为 32 和大小为 4 的字节数组的长度相同。 始终“[@B1234567”。

这是什么? 此外,它们还具有仅打印十六进制值的属性。 我知道它不能只是二进制打印,因为会出现随机扩展的 ascii 字符。

下面是 byte[] 到 byte[] 哈希表映射打印的示例,其中映射由冒号分隔,这些是 4 字节键和 32 字节元素的字节数组。

[B@1ef9157:[B@1f82982
[B@181ed9e:[B@16d2633
[B@27e353:[B@e70e30
[B@cb6009:[B@154864a
[B@18aaa1e:[B@3c9217
[B@20be79:[B@9b42e6
[B@16925b0:[B@14520eb
[B@8ee016:[B@1742700
[B@1bfc93a:[B@acb158
[B@107ebe1:[B@1af33d6
[B@156b6b9:[B@17431b9
[B@139b78e:[B@16c79d7
[B@2e7820:[B@b33d0a
[B@82701e:[B@16c9867
[B@1f14ceb:[B@89cc5e
[B@da4b71:[B@c837cd
[B@ab853b:[B@c79809
[B@765a16:[B@1ce784b
[B@1319c:[B@3bc473

Hey, I'm trying to figure out what the [B@ prefix means in java. They come out when I attempt to print byte arrays. However, byte arrays of size 32 and size 4 are identical in length. Always "[@B1234567".

What is this? Also, they have the property of only printing hex values. I know it can't just be a binary print because random extended ascii chars would appear.

Here is an example of a byte[] to byte[] hashtable mapping print, where mappings are separated by a colon, and these are byte arrays of 4-byte keys and 32-byte elements.

[B@1ef9157:[B@1f82982
[B@181ed9e:[B@16d2633
[B@27e353:[B@e70e30
[B@cb6009:[B@154864a
[B@18aaa1e:[B@3c9217
[B@20be79:[B@9b42e6
[B@16925b0:[B@14520eb
[B@8ee016:[B@1742700
[B@1bfc93a:[B@acb158
[B@107ebe1:[B@1af33d6
[B@156b6b9:[B@17431b9
[B@139b78e:[B@16c79d7
[B@2e7820:[B@b33d0a
[B@82701e:[B@16c9867
[B@1f14ceb:[B@89cc5e
[B@da4b71:[B@c837cd
[B@ab853b:[B@c79809
[B@765a16:[B@1ce784b
[B@1319c:[B@3bc473

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

晨曦÷微暖 2024-08-02 04:45:34

您正在查看对象 ID,而不是内容转储。

  • [ 表示数组。
  • B 表示字节。
  • @ 将类型与 ID 分开。
  • 十六进制数字是对象 ID 或哈希码。

如果目的是打印数组的内容,有很多方法。 例如:

byte[] in = new byte[] { 1, 2, 3, -1, -2, -3 };
System.out.println(byteArrayToString(in));

String byteArrayToString(byte[] in) {
    char out[] = new char[in.length * 2];
    for (int i = 0; i < in.length; i++) {
        out[i * 2] = "0123456789ABCDEF".charAt((in[i] >> 4) & 15);
        out[i * 2 + 1] = "0123456789ABCDEF".charAt(in[i] & 15);
    }
    return new String(out);
}

完整列表 类型命名法可以在 JNI 文档

以下是完整列表:

  • B - 字节
  • C - 字符
  • D - 双精度
  • F - 浮点
  • I - int
  • J - long
  • L***完全限定类*;** - 介于 L 和 < code>; 是完整的类名,使用 / 作为包之间的分隔符(例如,Ljava/lang/String;
  • S< /strong> - 短
  • Z - 布尔值
  • [ - 数组的每个维度都有一个 [
  • (***参数类型*< /strong>)***return-type* - 方法签名,例如 (I)V,带有用于 void 方法的附加伪类型 V

You're looking at the object ID, not a dump of the contents.

  • The [ means array.
  • The B means byte.
  • The @ separates the type from the ID.
  • The hex digits are an object ID or hashcode.

If the intent is to print the contents of the array, there are many ways. For example:

byte[] in = new byte[] { 1, 2, 3, -1, -2, -3 };
System.out.println(byteArrayToString(in));

String byteArrayToString(byte[] in) {
    char out[] = new char[in.length * 2];
    for (int i = 0; i < in.length; i++) {
        out[i * 2] = "0123456789ABCDEF".charAt((in[i] >> 4) & 15);
        out[i * 2 + 1] = "0123456789ABCDEF".charAt(in[i] & 15);
    }
    return new String(out);
}

A complete list of the type nomenclature can be found in the JNI documentation.

Here is the entire list:

  • B - byte
  • C - char
  • D - double
  • F - float
  • I - int
  • J - long
  • L***fully-qualified-class*;** - between an L and a ; is the full class name, using / as the delimiter between packages (for example, Ljava/lang/String;)
  • S - short
  • Z - boolean
  • [ - one [ for every dimension of the array
  • (***argument types*)***return-type* - method signature, such as (I)V, with the additional pseudo-type of V for void method
真心难拥有 2024-08-02 04:45:34

[B@ 表示“字节数组”。 其他基本数组类型具有不同的前缀:

class Test
{   
    public static void main(String [] args)
    {
        byte[] b = new byte[0];
        int[] i = new int[0];
        char[] c = new char[0];
        long[] l = new long[0];
        double[] d = new double[0];
        float[] f = new float[0];
        short[] s = new short[0];        

        System.out.println(b);
        System.out.println(i);
        System.out.println(c.toString());
        System.out.println(l);
        System.out.println(d);
        System.out.println(f);
        System.out.println(s);
    }
}

打印:

[B@3e25a5
[I@19821f
[C@addbf1
[J@42e816
[D@9304b1
[F@190d11
[S@a90653

非基本类型包括 [L 之后的类型名称,例如:

[Ljava.lang.String;@a90653
[Ljava.lang.Object;@de6ced

如果您想将字节数组的内容打印为十六进制,这里有一些代码可以帮助您你:

class Test
{   
    public static void main(String [] args)
    {
        byte[] b = new byte[] { (byte) 0xf3, (byte) 0xf1, (byte) 0x7f };
        System.out.println(toHex(b));
    }

    private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
    public static String toHex(byte[] bytes)
    {
        char[] c = new char[bytes.length*2];
        int index = 0;
        for (byte b : bytes)
        {
            c[index++] = HEX_DIGITS[(b >> 4) & 0xf];
            c[index++] = HEX_DIGITS[b & 0xf];
        }
        return new String(c);
    }
}

[B@ means "byte array". Other primitive array types have different prefixes:

class Test
{   
    public static void main(String [] args)
    {
        byte[] b = new byte[0];
        int[] i = new int[0];
        char[] c = new char[0];
        long[] l = new long[0];
        double[] d = new double[0];
        float[] f = new float[0];
        short[] s = new short[0];        

        System.out.println(b);
        System.out.println(i);
        System.out.println(c.toString());
        System.out.println(l);
        System.out.println(d);
        System.out.println(f);
        System.out.println(s);
    }
}

Prints:

[B@3e25a5
[I@19821f
[C@addbf1
[J@42e816
[D@9304b1
[F@190d11
[S@a90653

Non-primitive types include the type name after [L for instance:

[Ljava.lang.String;@a90653
[Ljava.lang.Object;@de6ced

If you want to print the contents of a byte array as hex, here's some code to help you:

class Test
{   
    public static void main(String [] args)
    {
        byte[] b = new byte[] { (byte) 0xf3, (byte) 0xf1, (byte) 0x7f };
        System.out.println(toHex(b));
    }

    private static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
    public static String toHex(byte[] bytes)
    {
        char[] c = new char[bytes.length*2];
        int index = 0;
        for (byte b : bytes)
        {
            c[index++] = HEX_DIGITS[(b >> 4) & 0xf];
            c[index++] = HEX_DIGITS[b & 0xf];
        }
        return new String(c);
    }
}
享受孤独 2024-08-02 04:45:34

默认的 toString() 实现是类名,后跟“@”,然后是对象的哈希码(十六进制)。

反过来,默认哈希码“通常通过将对象的内部地址转换为整数来实现”。 实际上,Sun JVM 使用对象句柄的地址作为输入来生成默认哈希码。

对于基本类型(intchar 等)或数组类型(例如 byte[]),在 字段描述符的 Java 虚拟机规范是用过的。 根据这些规则,一个“[”表示一维数组,而“B”表示byte的组件类型。

The default toString() implementation is the class name, followed by '@', followed by the object's hash code (in hexadecimal).

The default hash code, in turn, is "typically implemented by converting the internal address of the object into an integer". In practice, the Sun JVM uses the address of an object handle as input to generate the default hash code.

In the case of primitive types (int, char, etc.) or array types like byte[], naming rules defined in the Java Virtual Machine Specification for field descriptors are used. According to those rules, one '[' indicates an array of one dimension, while 'B' indicates a component type of byte.

挽梦忆笙歌 2024-08-02 04:45:34

我怀疑(尽管我不知道)十六进制字符串是内存中实例地址的表示,并且可能与数组的长度关系不大。 你能澄清你的问题吗?

I suspect, though I don't know, that the hex strings are representations of the instances' addresses in memory, and probably have little to do with the lengths of the arrays. Can you clarify your question?

默嘫て 2024-08-02 04:45:34

值得注意的是 equals() 来自 Object,因此如果 a.equals(b) then a == bie 如果你有两个包含相同数据的字节数组,它们不是 equals() 并且不会匹配 a 中的键哈希表、HashXxxx

It worth noting that equals() comes from Object, so that if a.equals(b) then a == b. i.e. if you have two byte arrays which contain the same data, they are not equals() and will not match keys in a Hashtable, HashXxxx

零崎曲识 2024-08-02 04:45:34

我在使用csv输入组件时遇到了这个问题。 经过几个小时后,我发现这些值是字节。 我解决了这个问题,在“选择值”组件上将参数“二进制”选择“是”以正常。

I had this problem when I used csv input component. After a lot of hours I discovered that the values are bytes. I resolved this selecting Yes for the parameter Binary to normal on Select Value component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文