How to convert byte array to byte array
How to convert byte array to byte array
Java Byte Array to String to Byte Array
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.
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):
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
The server should return this data to the client, where it can be verified.
The response my client receives (as a string) looks like
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.
or I can get a byte representation which is as follows:
Both of these are different from my sent data. I’m sure Im missing something truly simple.
12 Answers 12
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
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 :
** EDIT **
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 :
That outputs «cool string» to the console.
It’s pretty darn easy.
return to clients:
receive from clients:
your data will be transferred in this format:
What Arrays.toString() does is create a string representation of each individual byte in your byteArray.
Please check the API documentation Arrays API
To convert your response string back to the original byte array, you have to use split(«,») or something and convert it into a collection and then convert each individual item in there to a byte to recreate your byte array.
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:
The kind of output you are seeing from your byte array ( [B@405217f8 ) is also an output for a zero length byte array (ie new 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’s toString() method.
As with other respondents, I would point you to the String constructors that accept a byte[] parameter to construct a string from the contents of a byte array. You should be able to read raw bytes from a socket’s InputStream if you want to obtain bytes from a TCP connection.
If you want to deal with raw bytes you should really avoid using this stream reader layer.
How to convert byte[] to Byte[] and the other way around?
Is there a way to do it fast just using the standard library?
8 Answers 8
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Java 8 solution:
From byte[] to Byte[]:
From Byte[] to byte[] (using our previously-defined B ):
If someone preferes Stream API over ordinary loops.
Step back. Look at the bigger picture. You’re stuck converting byte[] to Byte[] or vice versa because of Java’s strict type casing with something like this
Now you have byte[] and Byte[] and have to convert. This will help.
As you acquire bytes do this (networking socket example):
Then, when you want to put all your bytes in a single message, do this:
That way, you can keep all your parts in List and your whole in byte[] without a bunch of crazy copy tasks in for loops with little baby byte[]s everywhere. 😉