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.
When I send the data from my Java application...
Arrays.toString(data.toByteArray())
Bytes to send..
[B@405217f8
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]
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....
Any help?!
The kind of output you are seeing from your byte array (
[B@405217f8
) is also an output for a zero length byte array (ienew byte[0]
). It looks like this string is a reference to the array rather than a description of the contents of the array like we might expect from a regular collection'stoString()
method.As with other respondents, I would point you to the
String
constructors that accept abyte[]
parameter to construct a string from the contents of a byte array. You should be able to read raw bytes from a socket'sInputStream
if you want to obtain bytes from a TCP connection.If you have already read those bytes as a
String
(using anInputStreamReader
), then, the string can be converted to bytes using thegetBytes()
function. Be sure to pass in your desired character set to both the String constructor andgetBytes()
functions, and this will only work if the byte data can be converted to characters by theInputStreamReader
.If you want to deal with raw bytes you should really avoid using this stream reader layer.
If you want to convert the string back into a byte array you will need to use
String.getBytes()
(or equivalent Python function) and this will allow you print out the original byte array.What I did:
return to clients:
receive from clients:
your data will be transferred in this format:
Its simple to convert byte array to string and string back to byte array in java. we need to know when to use 'new' in the right way. It can be done as follows:
byte array to string conversion:
String to byte array conversion:
For more details, look at: http://evverythingatonce.blogspot.in/2014/01/tech-talkbyte-array-and-string.html
[JAVA 8]