Binary To Text using Base64 encoding

If you need to convert binary data to text one of the recommended approach is to use base64 encoding and not direct string conversion like
byte[] byteArray = //get byte array from any image file
String str = new String(byteArray);

This is not recommended as it will not necessarily represent a valid character string in any encoding. Base64 will make sure that the byte array represents a valid ASCII character string
Ex byte[] byteArray = //get byte array from any image file
String str = new Base64Encoder().encode(byteArray);

To support this point lets take an XML String with the str String padded between some xml element like
String xmlString = <imagedata>;

This xml when parsed for the image
InputSource inputSource = new InputSource(new StringReader(xmlString));
DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder();
Document document = builder.parse(inputSource);

will throw an error in the first case ie using String saying
org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the element content of the document.

So a cleaner approach is to use Base64 encoding.


Popular posts from this blog

How does HashMap work in Java

Immutable classes

Equals and hashcode method