Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Java String into byte[]?

Is there any way to convert Java String to a byte[] (not the boxed Byte[])?

In trying this:

System.out.println(response.split("\r\n\r\n")[1]); System.out.println("******"); System.out.println(response.split("\r\n\r\n")[1].getBytes().toString()); 

and I'm getting separate outputs. Unable to display 1st output as it is a gzip string.

<A Gzip String> ****** [B@38ee9f13 

The second is an address. Is there anything I'm doing wrong? I need the result in a byte[] to feed it to gzip decompressor, which is as follows.

String decompressGZIP(byte[] gzip) throws IOException {     java.util.zip.Inflater inf = new java.util.zip.Inflater();     java.io.ByteArrayInputStream bytein = new java.io.ByteArrayInputStream(gzip);     java.util.zip.GZIPInputStream gzin = new java.util.zip.GZIPInputStream(bytein);     java.io.ByteArrayOutputStream byteout = new java.io.ByteArrayOutputStream();     int res = 0;     byte buf[] = new byte[1024];     while (res >= 0) {         res = gzin.read(buf, 0, buf.length);         if (res > 0) {             byteout.write(buf, 0, res);         }     }     byte uncompressed[] = byteout.toByteArray();     return (uncompressed.toString()); } 
like image 626
Mkl Rjv Avatar asked Sep 02 '13 10:09

Mkl Rjv


People also ask

Which method converts string to byte value syntax?

The simplest way to do so is using parseByte() method of Byte class in java.

How do I convert a string to ByteArrayOutputStream?

Now, if you want to write your String into a ByteArrayOutputStream or any other kind of OutputStream you could simple use the String. getBytes() method.

How do I add a string to a byte array?

Converting String to byte[] in JavaString class has getBytes() method which can be used to convert String to byte array in Java. getBytes()- Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.


1 Answers

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.

like image 69
Stewart Avatar answered Oct 15 '22 00:10

Stewart