How can we convert Strings to and from UTF8 byte arrays?

0 votes
I have a String and I want to encode it using UT8 encoding. I want to decode the same data. How can I do this?
Jun 21, 2018 in Java by Daisy
• 8,040 points
25 views

1 answer to this question.

0 votes

To convert STring to byte[]:

String s = "some text here";
byte[] b = s.getBytes("UTF-8");

To convert byte[] to STrings:

byte[] b = {(byte) 99, (byte)97, (byte)116};
String s = new String(b, "US-ASCII");
answered Jun 21, 2018 by Akrati
• 3,150 points

Related Questions In Java

0 votes
1 answer

How to convert byte array to String and STring to byte array?

The conversion between byte[] and String is to explicitly state the ...READ MORE

answered May 14, 2018 in Java by parth295
• 4,600 points
38 views
0 votes
1 answer

How can we convert java.util.Date to java.sql.date in Java?

Use java.time classes instead of java.util.Date & java.sql.Date with JDBC 4.2 or ...READ MORE

answered Jun 29, 2018 in Java by Sushmita
• 6,840 points
63 views
0 votes
2 answers

How do I read and convert an InputStream object to string type?

You can also use Java Standard Library ...READ MORE

answered Jul 17, 2018 in Java by Sushmita
• 6,840 points
2,272 views
0 votes
1 answer

How can I convert an 'ArrayList<String> to 'String[]' in Java

List<String> list = new ArrayList<String>(); String[] array = ...READ MORE

answered Apr 20, 2018 in Java by Daisy
• 8,040 points
113 views
0 votes
1 answer

How to convert a java string into byte array

You can probably use ByteBuffer:  byte[] byte = ...READ MORE

answered Jun 21, 2018 in Java by Perry
• 17,010 points
36 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,320 points
83 views
0 votes
2 answers

Integer to String conversion in java

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

answered Jul 27, 2018 in Java by samarth295
• 2,190 points
57 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,840 points
40 views
0 votes
1 answer

How can we add leading zeros to the number in Java?

To add zeros to a number, we ...READ MORE

answered May 25, 2018 in Java by Akrati
• 3,150 points
141 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,150 points
1,547 views