Java String to byte [](String包含对象数据)

时间:2022-08-18 17:03:28

I am using a JavaScript to Applet object called JSObject and I get from my JSObject the value of a java object that I stored in my html page.

我正在使用一个名为JSObject的JavaScript to Applet对象,我从我的JSObject获取了我存储在html页面中的java对象的值。

The java object is a byte[] but JavaScript converts it to a String.

java对象是byte []但JavaScript将其转换为String。

So in the HTML page: object value = [B@ca0b6

所以在HTML页面中:object value = [B @ ca0b6

In the Applet, the String value is also [B@ca0b6

在Applet中,String值也是[B @ ca0b6

Is there a way for me to convert this String value of [B@ca0b6 into the byte representation? I don't mean String.getByte() because that will convert the STRING [B@ca0b6 into byte[] data.

有没有办法让我将[B @ ca0b6的字符串值]转换成字节表示?我不是指String.getByte(),因为它会将STRING [B @ ca0b6转换为byte []数据。

Thanks!

谢谢!

2 个解决方案

#1


3  

No, you can't. This is the default toString() method, which does not output anything of the array contents. It contains only the type of the object (array of bytes) and the memory address within the JVM, in hex.

不,你不能。这是默认的toString()方法,它不输出任何数组内容。它仅包含对象的类型(字节数组)和JVM中的内存地址(十六进制)。

If you want to convert your array to String properly, use Arrays.toString(array)

如果要将数组正确转换为String,请使用Arrays.toString(array)

#2


1  

You can use:

您可以使用:

new String(bytearray, "UTF-8")

(change UTF-8 to something else (e.g., ISO-8859-1) if your bytes are not UTF-8.)

(如果您的字节不是UTF-8,则将UTF-8更改为其他内容(例如,ISO-8859-1)。)

#1


3  

No, you can't. This is the default toString() method, which does not output anything of the array contents. It contains only the type of the object (array of bytes) and the memory address within the JVM, in hex.

不,你不能。这是默认的toString()方法,它不输出任何数组内容。它仅包含对象的类型(字节数组)和JVM中的内存地址(十六进制)。

If you want to convert your array to String properly, use Arrays.toString(array)

如果要将数组正确转换为String,请使用Arrays.toString(array)

#2


1  

You can use:

您可以使用:

new String(bytearray, "UTF-8")

(change UTF-8 to something else (e.g., ISO-8859-1) if your bytes are not UTF-8.)

(如果您的字节不是UTF-8,则将UTF-8更改为其他内容(例如,ISO-8859-1)。)