I want to translate each byte
from byte[]
to char
, and then put these characters in a string. This is the so-called "binary" encoding of some databases. So far, the best I could find is a huge template:
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 maybe from Apache Commons? I wish I had something like this:
final Charset BINARY_CS = Charset.forName("BINARY"); String s = new String(bytes, BINARY_CS);
But I do not want to write Charset and their codecs (for now). Is there any ready-made binary encoding in the JRE or in Apache Commons?
source share