Convert String type to byte in Java

0 votes

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());
}
May 8, 2018 in Java by developer_1
• 3,320 points
586 views

1 answer to this question.

0 votes

Try using String.getBytes(). It returns a byte[] representing string data. Example:

String data = "sample data";
byte[] byteData = data.getBytes();
answered May 8, 2018 by Rishabh
• 3,620 points

Related Questions In Java

+16 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
250,608 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 15, 2018 in Java by samarth295
• 2,220 points
1,960 views
0 votes
2 answers

How to convert ArrayList<String> to String[] in Java

Try this:  String[] strArray = arrayList.toArray(new Str ...READ MORE

answered Aug 21, 2019 in Java by Sirajul
• 59,230 points
749 views
0 votes
2 answers

How can I convert byte array into hex string in Java?

public static String byteArrayToHex(byte[] a) { ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
2,604 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
1,915 views
0 votes
4 answers

Remove extra spaces from string in java

import java.util.regex.Matcher; import java.util.regex.Pattern; String pattern="[\\s]"; String replace=""; part="name=john age=13 year=2001"; Pattern ...READ MORE

answered Sep 10, 2018 in Java by Parth
• 4,630 points
2,683 views
0 votes
2 answers
0 votes
2 answers

How do I convert an input stream into a byte array

You could probably try this if you ...READ MORE

answered Aug 29, 2019 in Java by Sirajul
• 59,230 points
3,367 views
0 votes
2 answers

Integer to String conversion in java

We can do this in 2 ways: String ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
853 views
0 votes
3 answers

String to Double conversion in java

Double temp = Double.valueOf(str); number = temp.doubleValue(); READ MORE

answered Sep 10, 2018 in Java by Sushmita
• 6,910 points
1,334 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP