Your byte array must have some encoding. The encoding cannot be ASCII if you've got negative values. Once you figure that out, you can convert a set of bytes to a String using:
byte[] bytes = {...}
String str = new String(bytes, StandardCharsets.UTF_8); // for UTF-8 encoding
There are a bunch of encodings you can use, look at the supported encodings in the Oracle javadocs.
Answer from omerkudat on Stack OverflowYour byte array must have some encoding. The encoding cannot be ASCII if you've got negative values. Once you figure that out, you can convert a set of bytes to a String using:
byte[] bytes = {...}
String str = new String(bytes, StandardCharsets.UTF_8); // for UTF-8 encoding
There are a bunch of encodings you can use, look at the supported encodings in the Oracle javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
If you really must use a String to hold binary data then the safest way is to use Base64 encoding.
arrays - How to convert Java String into byte[]? - Stack Overflow
How do you convert an "Byte array to string" text array back to normal text?
Java Byte Array to String to Byte Array - Stack Overflow
converting large byte array back to string
Videos
The object your method decompressGZIP() needs is a byte[].
So the basic, technical answer to the question you have asked is:
byte[] b = string.getBytes();
byte[] b = string.getBytes(Charset.forName("UTF-8"));
byte[] b = string.getBytes(StandardCharsets.UTF_8); // Java 7+ only
However the problem you appear to be wrestling with is that this doesn't display very well. Calling toString() will just give you the default Object.toString() which is the class name + memory address. In your result [B@38ee9f13, the [B means byte[] and 38ee9f13 is the memory address, separated by an @.
For display purposes you can use:
Arrays.toString(bytes);
But this will just display as a sequence of comma-separated integers, which may or may not be what you want.
To get a readable String back from a byte[], use:
String string = new String(byte[] bytes, Charset charset);
The reason the Charset version is favoured, is that all String objects in Java are stored internally as UTF-16. When converting to a byte[] you will get a different breakdown of bytes for the given glyphs of that String, depending upon the chosen charset.
String example = "Convert Java String";
byte[] bytes = example.getBytes();
I got a string called abc, I got the bytes of abc and put them into the byte array named b, then i created a string which converts byte array b to string via Arrays.toString(b). This results in [97, 98, 99, 100, 101, 102, 103, 104, 105, 103, 104, 116].
How would you reverse this? (it's a seperate function).
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);
** EDIT **
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.
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";
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.
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.
String coolString = "cool string";
byte[] byteArray = coolString.getBytes();
String reconstitutedString = new String(byteArray);
System.out.println(reconstitutedString);
That outputs "cool string" to the console.
It's pretty darn easy.
So normally you can create a byte array as a variable something like
byte[] bytes = {69, 121, 101, ...};
but I have a huge one that blows up method/class file if I try this and wont compile. I've put it in a text file and trying to read it in, but now its coming as a string literal such as "69, 121, 101, ..."
if i try to use a readAllBytes method, its basically converting the above string to bytes which is now not matching and looks totally different like 49, 43, 101, .... so now its a byte array of a string-ified byte array if that makes sense.
i've managed to get it back to a byte array and then string, but it seems to be a janky way and wondering if theres a more proper way.
currently i'm
-
reading the whole string into memory
-
using string.split(",")
-
converting string value to int
-
converting int to byte
-
add to byte array
-
new String(myByteArray)
this works, but is it really the only way to do this?
You need to specify the encoding you want e.g. for UTF-8
String doc = ....
byte[] bytes = doc.getBytes("UTF-8");
String doc2 = new String(bytes, "UTF-8");
doc and doc2 will be the same.
To decode a byte[] you need to know what encoding was used to be sure it will decode correctly.
Here's one way to convert an array of bytes into a String and back:
String doc=new String(bytes, "ISO-8859-1");
byte[] bytes2=doc.getBytes("ISO-8859-1");
A String is a sequence of characters, so you'll have to somehow encode bytes as characters. The ISO-8859-1 encoding maps a single, unique character for each byte, so it's safe to use it for the conversion. Note that other encodings, such as UTF-8, are not safe in this sense because there are sequences of bytes that don't map to valid strings in those encodings.