Java 字节数组到字符串到字节数组 [英] Java Byte Array to String to Byte Array
问题描述
我试图理解字节[]到字符串,字节[]到字节[]转换的字符串表示......我将我的字节[]转换为要发送的字符串,然后我期望我的网络服务(写在 python 中)将数据直接回显给客户端.
I'm trying to understand a byte[] to string, string representation of byte[] to byte[] conversion... I convert my byte[] to a string to send, I then expect my web service (written in python) to echo the data straight back to the client.
当我从 Java 应用程序发送数据时...
When I send the data from my Java application...
Arrays.toString(data.toByteArray())
要发送的字节数..
[B@405217f8
发送(这是 Arrays.toString() 的结果,它应该是我的字节数据的字符串表示,该数据将通过网络发送):
Send (This is the result of Arrays.toString() which should be a string representation of my byte data, this data will be sent across the wire):
[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]
在python端,python服务器向调用者返回一个字符串(我可以看到和我发送给服务器的字符串相同
On the python side, the python server returns a string to the caller (which I can see is the same as the string I sent to the server
[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]
服务器应该将此数据返回给客户端,在那里可以对其进行验证.
The server should return this data to the client, where it can be verified.
我的客户收到的响应(作为字符串)看起来像
The response my client receives (as a string) looks like
[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]
我似乎无法弄清楚如何将接收到的字符串重新转换为字节[]
I can't seem to figure out how to get the received string back into a byte[]
无论我似乎尝试什么,我最终都会得到一个字节数组,如下所示......
Whatever I seem to try I end up getting a byte array which looks as follows...
[91, 45, 52, 55, 44, 32, 49, 44, 32, 49, 54, 44, 32, 56, 52, 44, 32, 50, 44, 32, 49, 48, 49, 44, 32, 49, 49, 48, 44, 32, 56, 51, 44, 32, 49, 49, 49, 44, 32, 49, 48, 57, 44, 32, 49, 48, 49, 44, 32, 51, 50, 44, 32, 55, 56, 44, 32, 55, 48, 44, 32, 54, 55, 44, 32, 51, 50, 44, 32, 54, 56, 44, 32, 57, 55, 44, 32, 49, 49, 54, 44, 32, 57, 55, 93]
或者我可以得到如下的字节表示:
or I can get a byte representation which is as follows:
B@2a80d889
这两个都与我发送的数据不同......我确定我遗漏了一些真正简单的东西......
Both of these are different from my sent data... I'm sure Im missing something truly simple....
有什么帮助吗?!
推荐答案
你不能只获取返回的字符串并从中构造一个字符串...它不是 byte[]
数据类型不再,它已经是一个字符串;你需要解析它.例如:
You can't just take the returned string and construct a string from it... it's not a byte[]
data type anymore, it's already a string; you need to parse it. For example :
String response = "[-47, 1, 16, 84, 2, 101, 110, 83, 111, 109, 101, 32, 78, 70, 67, 32, 68, 97, 116, 97]"; // response from the Python script
String[] byteValues = response.substring(1, response.length() - 1).split(",");
byte[] bytes = new byte[byteValues.length];
for (int i=0, len=bytes.length; i<len; i++) {
bytes[i] = Byte.parseByte(byteValues[i].trim());
}
String str = new String(bytes);
** 编辑 **
你在你的问题中得到了一个提示,你说无论我尝试什么,我最终都会得到一个字节数组,如下所示...... [91, 45, ...
",因为91
是[
的字节值,所以[91, 45, ...
就是字符串的字节数组"[-45, 1, 16, ...
" 字符串.
You get an hint of your problem in your question, where you say "Whatever I seem to try I end up getting a byte array which looks as follows... [91, 45, ...
", because 91
is the byte value for [
, so [91, 45, ...
is the byte array of the string "[-45, 1, 16, ...
" string.
方法Arrays.toString()
将返回指定数组的String
表示;这意味着返回的值将不再是数组.例如:
The method Arrays.toString()
will return a String
representation of the specified array; meaning that the returned value will not be a array anymore. For example :
byte[] b1 = new byte[] {97, 98, 99};
String s1 = Arrays.toString(b1);
String s2 = new String(b1);
System.out.println(s1); // -> "[97, 98, 99]"
System.out.println(s2); // -> "abc";
如您所见,s1
保存了 array b1
的字符串表示,而 s2
保存了b1
中包含的字节的字符串表示.
As you can see, s1
holds the string representation of the array b1
, while s2
holds the string representation of the bytes contained in b1
.
现在,在您的问题中,您的服务器返回一个类似于 s1
的字符串,因此要返回数组表示,您需要相反的构造函数方法.如果s2.getBytes()
与new String(b1)
相反,则需要找到Arrays.toString(b1)
的相反数,因此我粘贴在这个答案的第一个片段中的代码.
Now, in your problem, your server returns a string similar to s1
, therefore to get the array representation back, you need the opposite constructor method. If s2.getBytes()
is the opposite of new String(b1)
, you need to find the opposite of Arrays.toString(b1)
, thus the code I pasted in the first snippet of this answer.
这篇关于Java 字节数组到字符串到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!