List of utility methods to do Byte Array to String by Charset
byte[] | asBytes(String string, Charset charset) Turn a string into an array of bytes using the passed Charset if (string == null) { return null; if (charset == null) { throw new IllegalArgumentException("Cannot get bytes without a Charset"); try { return string.getBytes(charset.name()); ... |
String | bytesToString(byte[] bytes, Charset charset) bytes To String return new String(bytes, charset); |
String | byteToString(byte[] b, String charset) byte To String try { return new String(b, charset); } catch (UnsupportedEncodingException e) { return null; |
String | byteToString(Charset charset, byte[] data) byte To String CharBuffer buffer = charset.decode(ByteBuffer.wrap(data)); String str = new String(buffer.array()); return str; |
byte[] | changeEncoding(byte[] byteArray, Charset charsetFrom, Charset charsetTo) Usage: Charset charsetFrom = Charset.forName("UTF-8"); Charset charsetTo = Charset.forName("ISO-8859-15"); ByteBuffer inputBuffer = ByteBuffer.wrap(byteArray);
CharBuffer data = charsetFrom.decode(inputBuffer);
ByteBuffer outputBuffer = charsetTo.encode(data);
return outputBuffer.array();
|
byte[] | charSequence2Bytes(CharSequence sb, Charset charset) char Sequence Bytes ByteBuffer bb = charSequence2ByteBuffer(sb, charset);
return byteBuffer2ByteArray(bb);
|
Optional | charSetForBOM(byte[] buffer, int offset) Determine UTF character set via byte order mark if (buffer.length < 2 + offset) { return Optional.empty(); int o = +offset; if ((buffer[o] == (byte) 0xFE) && (buffer[o + 1] == (byte) 0xFF)) { return Optional.of(StandardCharsets.UTF_16BE); if ((buffer[o] == (byte) 0xFF) && (buffer[o + 1] == (byte) 0xFE)) { ... |
byte[] | convertCharset(byte[] content, Charset fromCharset, Charset toCharset) convert Charset if (content == null || content.length == 0) return null; if (fromCharset == null || toCharset == null) { throw new IllegalArgumentException("Charset cannot be null!"); CharBuffer inputContent = fromCharset.decode(ByteBuffer.wrap(content)); ByteBuffer result = toCharset.encode(inputContent); return Arrays.copyOf(result.array(), result.limit()); ... |
byte[] | convertToBytes(final String str, final Charset charset) convert To Bytes return str.getBytes(charset);
|
char[] | convertToChars(final byte[] bytes, final Charset charset) convert To Chars ByteBuffer bb = ByteBuffer.allocate(bytes.length);
bb.put(bytes);
bb.flip();
CharBuffer cb = charset.decode(bb);
return cb.array();
|