Java - Convert byte[] to char[]. The encoding is UTF-16

3.7k views Asked by At

I need to convert byte[] to char[] in Java. The Unicode encoding used is UTF-16.

To be concise, I need a Java equivalent of c#'s

UnicodeEncoding.Unicode.GetChars(byte[] bytes);

Also, I need to convert only a part of byte[] to char[]

public virtual char[] GetChars(byte[] bytes, int index, int count);
3

There are 3 answers

0
M A On BEST ANSWER

You can try this:

byte[] b = ...
char[] c = new String(b, "UTF-16").toCharArray();

From String(byte[] bytes, String charsetName):

Constructs a new String by decoding the specified array of bytes using the specified charset.

0
fury.slay On

C# API:

public virtual char[] GetChars(byte[] bytes);

Java:

byte[] byte = ...

char[] char = new String(byte, "UTF-16").toCharArray();

Java methods to return char[] :

public static char[] getCharsFromBytes(byte[] bytes, String type) {
    try {
        String byteConvert = new String(bytes, "UTF-16");
        return byteConvert.toCharArray();
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(DataTypeUtil.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

C# API:

public virtual char[] GetChars(byte[] bytes, int index, int count);

Java:

Iterate through required bytes in the byte array

public static char[] getCharsFromBytes(byte[] bytes, int index, int count) {
    char[] charArr = getCharsFromBytes(bytes);
    char[] result = new char[count];
    for (int i = 0; i < count; i++) {
        result[i] = charArr[i + startIndex];
    }
    return result;
}
0
Silas S. Brown On
public static char[] GetChars(byte[] bytes, int index, int count) {
    try {
        return new String(java.util.Arrays.copyOfRange(bytes,index,index+count), "UTF-16").toCharArray();
    } catch (java.io.UnsupportedEncodingException e) {
        assert false: "Your JRE is broken (UTF-16 not supported)";
        return null;
    }
}