2 Examples To Convert Byte[] Array To String Inward Java

Converting a byte array to String seems slow but what is hard is, doing it correctly. Many programmers brand error of ignoring grapheme encoding whenever bytes are converted into a String or char or vice versa. As a programmer, nosotros all know that computer's entirely sympathise binary information i.e. 0 together with 1. All things nosotros encounter together with run e.g. images, text files, movies, or whatever other multi-media is stored inward shape of bytes, but what is to a greater extent than of import is procedure of encoding or decoding bytes to character. Data conversion is an of import topic on whatever programming interview, together with because of trickiness of grapheme encoding, this questions is 1 of the most popular String Interview question on Java Interviews. While reading a String from input origin e.g. XML files, HTTP request, network port, or database, yous must pay attending on which grapheme encoding (e.g. UTF-8, UTF-16, together with ISO 8859-1) they are encoded. If yous volition non run the same grapheme encoding spell converting bytes to String, yous would destination upwardly amongst a corrupt String which may incorporate totally wrong values. You mightiness receive got seen ?, foursquare brackets after converting byte[] to String, those are because of values your electrical flow grapheme encoding is non supporting, together with only showing around garbage values.

I tried to sympathise why programmes brand grapheme encoding mistakes to a greater extent than oftentimes than not, together with my footling enquiry together with ain sense suggests that, it may last because of 2 reasons, start non dealing plenty amongst internationalization together with grapheme encodings together with minute because ASCII characters are supported past times almost all pop encoding schemes together with has same values.  Since nosotros mostly bargain amongst encoding similar UTF-8Cp1252 and Windows-1252, which displays ASCII characters (mostly alphabets together with numbers) without fail, fifty-fifty if yous run different encoding scheme. Real lawsuit comes when your text contains special characters e.g. 'é', which is oftentimes used inward French names. If your platform's grapheme encoding doesn't recognize that grapheme therefore either yous volition encounter a different grapheme or something garbage, together with sadly until yous got your hands burned, yous are unlikely to last careful amongst grapheme encoding. In Java, things are footling fighting to a greater extent than tricky because many IO classes e.g. InputStreamReader by default run platform's grapheme encoding. What this way is that, if yous run your programme inward different machine, yous volition probable acquire different output because of different grapheme encoding used on that machine. In this article, nosotros volition acquire how to convert byte[] to String inward Java both past times using JDK API together with amongst the aid of Guava together with Apache commons.




How to convert byte[] to String inward Java

There are multiple ways to modify byte array to String inward Java, yous tin give the axe either run methods from JDK, or yous tin give the axe run opened upwardly origin gratuitous APIs similar Apache park together with Google Guava. These API provides at to the lowest degree 2 sets of methods to do String shape byte array;  one, which uses default platform encoding together with other which takes grapheme encoding. You should ever run after one, don't rely on platform encoding. I know, it could last same or yous mightiness non receive got faced whatever employment therefore far, but it's ameliorate to last prophylactic than sorry. As I pointed out inward my concluding post virtually printing byte array every bit Hex String, It's likewise 1 of the best practise to specify grapheme encoding spell converting bytes to grapheme inward whatever programming language. It mightiness last possible that your byte array incorporate non-printable ASCII characters. Let's start encounter JDK's way of converting byte[] to String :

1) You tin give the axe run constructor of String, which takes byte array together with grapheme encoding

String str = new String(bytes, "UTF-8");

This is the right way to convert bytes to String, provided yous know for certain that bytes are encoded inward the grapheme encoding yous are using.

2) If yous are reading byte array from whatever text file e.g. XML document, HTML file or binary file, yous tin give the axe run the Apache Commons IO library to convert the FileInputStream to a String directly. This method likewise buffers the input internally, therefore at that topographic point is no hollo for to run around other BufferedInputStream.

String fromStream = IOUtils.toString(fileInputStream, "UTF-8");

In companionship to correctly convert those byte array into String, yous must start  discover right grapheme encoding past times reading meta information e.g. Content-Type<?xml encoding="…"> etc, depending on the format/protocol of the information yous are reading. This is 1 of the argue I recommend to run XML parsers e.g. SAX or DOM parsers to read XML files, they accept attention of grapheme encoding past times themselves.

Some programmers, likewise recommends to run Charset over String for specifying grapheme encoding,  e.g. instead of "UTF-8" run StandardCharsets.UTF_8 mainly to avoid UnsupportedEncodingException inward worst case. There are 6 measure Charset implementations guaranteed to last supported past times all Java platform implementations. You tin give the axe run them instead specifying encoding scheme inward String. In short, ever prefer StandardCharsets.ISO_8859_1 over "ISO_8859_1", every bit shown below :

String str = IOUtils.toString(fis,StandardCharsets.UTF_8);

Other measure charset supported past times Java platform are :

  1. StandardCharsets.ISO_8859_1
  2. StandardCharsets.US_ASCII
  3. StandardCharsets.UTF_16
  4. StandardCharsets.UTF_16BE
  5. StandardCharsets.UTF_16LE


If yous are reading bytes from input stream, yous tin give the axe likewise depository fiscal establishment lucifer my before post virtually 5 ways to convert InputStream to String inward Java for details.

Original XML
Here is our sample XML snippet to demonstrate issues amongst using default grapheme encoding. This file contains letter 'é'which is non correctly displayed inward Eclipse because it's default grapheme encoding is Cp1252.

xml version="1.0" encoding="UTF-8"?> <banks>     <bank>         <name>Industrial & Commercial Bank of mainland People's Republic of China </name>         <headquarters> Beijing , China</headquarters>     </bank>     <bank>         <name>Crédit Agricole SA</name>         <headquarters>Montrouge, France</headquarters>     </bank>     <bank>         <name>Société Générale</name>         <headquarters>Paris, Île-de-France, France</headquarters>     </bank> </banks>

And, this is what happens when yous convert a byte array to String without specify grapheme encoding, e.g. :

String str = new String(filedata);

This volition run platform's default grapheme encoding, which is Cp1252 in this case, because nosotros are running this programme inward Eclipse IDE. You tin give the axe encounter that letter 'é' is non displayed correctly.

xml version="1.0" encoding="UTF-8"?> <banks>     <bank>         <name>Industrial & Commercial Bank of mainland People's Republic of China </name>         <headquarters> Beijing , China</headquarters>     </bank>     <bank>         <name>Crédit Agricole SA</name>         <headquarters>Montrouge, France</headquarters>     </bank>     <bank>         <name>Société Générale</name>         <headquarters>Paris, Île-de-France, France</headquarters>     </bank> </banks>


To laid upwardly this, specify grapheme encoding spell creating String from byte array, e.g.

String str = new String(filedata, "UTF-8");

By the way, allow me acquire inward clear that fifty-fifty though I receive got read XML files using InputStream hither it's non a expert practice, inward fact it's a bad practice. You should ever run proper XML parsers for reading XML documents. If yous don't know how, delight depository fiscal establishment lucifer this tutorial. Since this instance is mostly to present yous why grapheme encoding matters, I receive got chosen an instance which was easily available together with looks to a greater extent than practical.


Java Program to Convert Byte array to String inward Java

 Converting a byte array to String seems slow but what is hard is 2 Examples to Convert Byte[]  Array to String inward Java
Here is our sample programme to present why relying on default grapheme encoding is a bad thought together with why yous must run grapheme encoding spell converting byte array to String inward Java. In this program, nosotros are using Apache Commons IOUtils course of didactics to straight read file into byte array. It takes attention of opening/closing input stream, therefore yous don't hollo for to worry virtually leaking file descriptors. Now how yous do String using that array, is the key. If yous render right grapheme encoding, yous volition acquire right output otherwise a nearly right but wrong output.

import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.io.IOUtils;  /**  * Java Program to convert byte array to String. In this example, nosotros receive got start  * read an XML file amongst grapheme encoding "UTF-8" into byte array together with therefore created  * String from that. When yous don't specify a grapheme encoding, Java uses  * platform's default encoding, which may non last the same if file is a XML document coming from around other system, emails, or apparently text files fetched from an * HTTP server etc. You must start uncovering right grapheme encoding  * together with therefore run them spell converting byte array to String.  *  * @author Javin Paul  */ public class ByteArrayToString{          public static void main(String args[]) throws IOException  {             System.out.println("Platform Encoding : " + System.getProperty("file.encoding"));                            FileInputStream fis = new FileInputStream("info.xml");                       // Using Apache Commons IOUtils to read file into byte array            byte[] filedata = IOUtils.toByteArray(fis);                            String str = new String(filedata, "UTF-8");            System.out.println(str);                                         } }  Output : Platform Encoding : Cp1252 <?xml version="1.0" encoding="UTF-8"?> <banks>     <bank>         <name>Industrial & Commercial Bank of China </name>         <headquarters> Beijing , China</headquarters>     </bank>     <bank>         <name>Crédit Agricole SA</name>         <headquarters>Montrouge, France</headquarters>     </bank>     <bank>         <name>Société Générale</name>         <headquarters>Paris, Île-de-France, France</headquarters>     </bank> </banks>


Things to retrieve together with Best Practices

Always remember, using grapheme encoding spell converting byte array to String is non a best practise but mandatory thing. You should ever run it irrespective of programming language. By the way, yous tin give the axe accept depository fiscal establishment annotation of next things, which volition aid yous to avoid twain of nasty issues :

  • Use grapheme encoding from the origin e.g. Content-Type inward HTML files, or <?xml encoding="…">.
  • Use XML parsers to parse XML files instead of finding grapheme encoding together with reading it via InputStream, around things are best left for demo code only. 
  • Prefer Charset constants e.g. StandardCharsets.UTF_16 instead of String "UTF-16"
  • Never rely on platform's default encoding scheme

This rules should likewise last applied when yous convert grapheme information to byte e.g. converting String to byte array using String.getBytes() method. In this instance it volition run platform's default grapheme encoding, instead of this yous should run overloaded version which takes grapheme encoding.

That's all on how to convert byte array to String inward Java. As yous tin give the axe encounter that Java API, especially java.lang.String course of didactics provides methods together with constructor that takes a byte[] together with returns a String (or vice versa), but past times default they rely on platform's grapheme encoding, which may non last correct, if byte array is created from XML files, HTTP asking information or from network protocols. You should ever acquire right encoding from origin itself. If yous similar to read to a greater extent than virtually what every programmer should know virtually String, yous tin give the axe checkout this article.

Further Learning
Data Structures together with Algorithms: Deep Dive Using Java
Algorithms together with Data Structures - Part 1 together with 2
Data Structures inward Java ix past times Heinz Kabutz



0 Response to "2 Examples To Convert Byte[] Array To String Inward Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel