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

Converting a byte array to String seems slow but what is hard is, doing it correctly. Many programmers brand fault 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 alone sympathize binary information i.e. 0 in addition to 1. All things nosotros run across in addition to role e.g. images, text files, movies, or whatsoever 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 theme on whatsoever programming interview, in addition to 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 beginning e.g. XML files, HTTP request, network port, or database, yous must pay attending on which grapheme encoding (e.g. UTF-8, UTF-16, in addition to ISO 8859-1) they are encoded. If yous volition non role the same grapheme encoding piece converting bytes to String, yous would destination upward amongst a corrupt String which may comprise totally wrong values. You mightiness guide maintain seen ?, foursquare brackets after converting byte[] to String, those are because of values your electrical current grapheme encoding is non supporting, in addition to simply showing simply about garbage values.

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




How to convert byte[] to String inward Java

There are multiple ways to alter byte array to String inward Java, yous tin either role methods from JDK, or yous tin role opened upward beginning gratuitous APIs similar Apache green in addition to Google Guava. These API provides at to the lowest degree ii sets of methods to produce String shape byte array;  one, which uses default platform encoding in addition to other which takes grapheme encoding. You should ever role afterward one, don't rely on platform encoding. I know, it could hold out same or yous mightiness non guide maintain faced whatsoever work hence far, but it's ameliorate to hold out rubber than sorry. As I pointed out inward my terminal post virtually printing byte array every bit Hex String, It's besides 1 of the best practise to specify grapheme encoding piece converting bytes to grapheme inward whatsoever programming language. It mightiness hold out possible that your byte array comprise non-printable ASCII characters. Let's showtime run across JDK's way of converting byte[] to String :

1) You tin role constructor of String, which takes byte array in addition to 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 whatsoever text file e.g. XML document, HTML file or binary file, yous tin role the Apache Commons IO library to convert the FileInputStream to a String directly. This method besides buffers the input internally, hence at that topographic point is no involve to role simply about other BufferedInputStream.

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

In club to correctly convert those byte array into String, yous must showtime  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 role XML parsers e.g. SAX or DOM parsers to read XML files, they guide maintain attention of grapheme encoding past times themselves.

Some programmers, besides recommends to role Charset over String for specifying grapheme encoding,  e.g. instead of "UTF-8" role StandardCharsets.UTF_8 mainly to avoid UnsupportedEncodingException inward worst case. There are 6 measure Charset implementations guaranteed to hold out supported past times all Java platform implementations. You tin role 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 besides depository fiscal establishment fit 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 Communist 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 role platform's default grapheme encoding, which is Cp1252 in this case, because nosotros are running this plan inward Eclipse IDE. You tin run across that letter 'é' is non displayed correctly.

xml version="1.0" encoding="UTF-8"?> <banks>     <bank>         <name>Industrial & Commercial Bank of Communist 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 produce this, specify grapheme encoding piece creating String from byte array, e.g.

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

By the way, allow me arrive clear that fifty-fifty though I guide maintain read XML files using InputStream hither it's non a skilful practice, inward fact it's a bad practice. You should ever role proper XML parsers for reading XML documents. If yous don't know how, delight depository fiscal establishment fit this tutorial. Since this illustration is mostly to exhibit yous why grapheme encoding matters, I guide maintain chosen an illustration which was easily available in addition to 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 plan to exhibit why relying on default grapheme encoding is a bad thought in addition to why yous must role grapheme encoding piece converting byte array to String inward Java. In this program, nosotros are using Apache Commons IOUtils course of written report to direct read file into byte array. It takes attention of opening/closing input stream, hence yous don't involve to worry virtually leaking file descriptors. Now how yous produce 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 guide maintain showtime  * read an XML file amongst grapheme encoding "UTF-8" into byte array in addition to and then created  * String from that. When yous don't specify a grapheme encoding, Java uses  * platform's default encoding, which may non hold out the same if file is a XML document coming from simply about other system, emails, or plainly text files fetched from an * HTTP server etc. You must showtime uncovering right grapheme encoding  * in addition to and then role them piece 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 shout upward in addition to Best Practices

Always remember, using grapheme encoding piece converting byte array to String is non a best practise but mandatory thing. You should ever role it irrespective of programming language. By the way, yous tin guide maintain depository fiscal establishment complaint of next things, which volition aid yous to avoid duet of nasty issues :

  • Use grapheme encoding from the beginning e.g. Content-Type inward HTML files, or <?xml encoding="…">.
  • Use XML parsers to parse XML files instead of finding grapheme encoding in addition to reading it via InputStream, simply about 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 besides hold out applied when yous convert grapheme information to byte e.g. converting String to byte array using String.getBytes() method. In this illustration it volition role platform's default grapheme encoding, instead of this yous should role overloaded version which takes grapheme encoding.

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

Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
Algorithms in addition to Data Structures - Part 1 in addition to 2
Data Structures inward Java nine past times Heinz Kabutz



Belum ada Komentar untuk "2 Examples To Convert Byte[] Array To String Inwards Java"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel