Java Utililty Methods Byte Array to String by Charset

List of utility methods to do Byte Array to String by Charset

Description

The list of methods to do Byte Array to String by Charset are organized into topic(s).

Method

byte[]stringToByteArray(String str, Charset charsetForEncoding, int lenOfByteArray, byte filler)
Converts the given String into a byte array of given length using the given Charset for encoding.
if (charsetForEncoding == null)
    charsetForEncoding = Charset.defaultCharset();
byte[] encoded = str != null && str.length() > 0 ? str.getBytes(charsetForEncoding) : new byte[] {};
byte[] result = new byte[lenOfByteArray];
for (int i = 0; i < lenOfByteArray; i++) {
    if (i < encoded.length) {
        result[i] = encoded[i];
    } else {
...
byte[]toBytes(CharSequence str, String charsetName)
Encodes the given string into a sequence of bytes using the named charset.
try {
    return str.toString().getBytes(charsetName);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);
byte[]toBytes(String string, Charset charset)
to Bytes
ByteBuffer buffer = charset.encode(string);
byte[] results = new byte[buffer.limit()];
buffer.get(results);
return results;
char[]toChars(byte[] b, String charset)
Converts byte array to char array.
return toChars(b, Charset.forName(charset));
byte[]toCharSet(byte[] bytes, String fromCharsetName, String toCharsetName)
to Char Set
if (bytes == null) {
    throw new IllegalArgumentException("bytes is null!");
if (toCharsetName == null || toCharsetName.trim().length() == 0) {
    throw new IllegalArgumentException("toCharsetName is null!");
if (fromCharsetName == null || fromCharsetName.trim().length() == 0) {
    fromCharsetName = System.getProperty("file.encoding");
...
StringtoDebugString(final byte[] bytes, final int len, final Charset charset)
to Debug String
if (charset == null) {
    return toDebugString(new String(bytes, 0, len, Charset.defaultCharset()));
return toDebugString(new String(bytes, 0, len, charset));
StringtoEncodedString(byte[] bytes, Charset charset)
Converts a byte[] to a String using the specified character encoding.
return new String(bytes, charset != null ? charset : Charset.defaultCharset());
StringtoString(byte[] b, String charset)
to String
return new String(b, Charset.forName(charset));
StringtoString(byte[] bytes, Charset charset)
to String
assertNotNull(charset, "charset");
if (bytes == null) {
    return null;
return charset.decode(ByteBuffer.wrap(bytes)).toString();
StringtoString(byte[] bytes, String charsetName)
Constructs a new String by decoding the given bytes using the specified charset.
try {
    return new String(bytes, charsetName);
} catch (UnsupportedEncodingException e) {
    throw new RuntimeException(e);