Java字节数组到字符串到字节数组 [英] Java Byte Array to String to Byte Array
问题描述
我正在尝试理解byte []到string,byte []到byte []转换的字符串表示...我将byte []转换为要发送的字符串,然后我希望我的Web服务(写入)在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
这些都与我发送的数据不同...我敢肯定我是mi真的很简单....
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,... $ c $]的字节数组c>string。
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()
将返回字符串
指定数组的表示;意味着返回的值不再是数组。例如:
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
持有数组的字符串表示 b1
,而 s2
包含 b1 中包含的> bytes 。
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屋!