fernacolo
fernacolo

Reputation: 7439

Convert byte[] to String using binary encoding

I want to translate each byte from a byte[] into a char, then put those chars on a String. This is the so-called "binary" encoding of some databases. So far, the best I could find is this huge boilerplate:

byte[] bytes = ...;
char[] chars = new char[bytes.length];
for (int i = 0; i < bytes.length; ++i) {
    chars[i] = (char) (bytes[i] & 0xFF);
}
String s = new String(chars);

Is there another option from Java SE or perhaps from Apache Commons? I wish I could have something like this:

final Charset BINARY_CS = Charset.forName("BINARY");
String s = new String(bytes, BINARY_CS);

But I'm not willing to write a Charset and their codecs (yet). Is there such a ready binary Charset in JRE or in Apache Commons?

Upvotes: 4

Views: 13574

Answers (4)

Chris Aldrich
Chris Aldrich

Reputation: 1932

You could skip the step of a char array and putting in String and even use a StringBuilder (or StringBuffer if you are worried about multi-threading). My example shows StringBuilder.

byte[] bytes = ...;
StringBuilder sb = new StringBuilder(bytes.length);
for (int i = 0; i < bytes.length; i++) {
  sb.append((char) (bytes[i] & 0xFF));
}

return sb.toString();

I know it doesn't answer your other question. Just seeking to help with simplifying the "boilerplate" code.

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533890

You could use the ASCII encoding for 7-bit characters

String s = "Hello World!";
byte[] b = s.getBytes("ASCII");
System.out.println(new String(b, "ASCII"));

or 8-bit ascii

String s = "Hello World! \u00ff";
byte[] b = s.getBytes("ISO-8859-1");
System.out.println(new String(b, "ISO-8859-1"));

EDIT

System.out.println("ASCII => " + Charset.forName("ASCII"));
System.out.println("US-ASCII => " + Charset.forName("US-ASCII"));
System.out.println("ISO-8859-1 => " + Charset.forName("ISO-8859-1"));

prints

ASCII => US-ASCII
US-ASCII => US-ASCII
ISO-8859-1 => ISO-8859-1

Upvotes: 9

havexz
havexz

Reputation: 9590

You can use base64 encoding. There is an implementation done by apache

http://commons.apache.org/codec/

Base 64 http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

Upvotes: 0

Andrew Rasmussen
Andrew Rasmussen

Reputation: 15109

There is a String constructor that takes an array of bytes and a string specifying the format of the bytes:

String s = new String(bytes, "UTF-8");   // if the charset is UTF-8
String s = new String(bytes, "ASCII");   // if the charset is ASCII

Upvotes: 0

Related Questions