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?
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With