Java - 将 byte[] 转换为 char[]。编码是UTF-16
Java - Convert byte[] to char[]. The encoding is UTF-16
我需要在 Java 中将 byte[] 转换为 char[]。
使用的 Unicode 编码是 UTF-16。
为了简洁起见,我需要一个 Java 相当于 c# 的
UnicodeEncoding.Unicode.GetChars(byte[] bytes);
另外,我只需要将byte[]的一部分转换成char[]
public virtual char[] GetChars(byte[] bytes, int index, int count);
你可以试试这个:
byte[] b = ...
char[] c = new String(b, "UTF-16").toCharArray();
来自String(byte[] bytes, String charsetName)
:
Constructs a new String
by decoding the specified array of bytes using the specified charset.
C#API:
public virtual char[] GetChars(byte[] bytes);
Java:
byte[] byte = ...
char[] char = new String(byte, "UTF-16").toCharArray();
Java 方法 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;
}
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;
}
}
我需要在 Java 中将 byte[] 转换为 char[]。 使用的 Unicode 编码是 UTF-16。
为了简洁起见,我需要一个 Java 相当于 c# 的
UnicodeEncoding.Unicode.GetChars(byte[] bytes);
另外,我只需要将byte[]的一部分转换成char[]
public virtual char[] GetChars(byte[] bytes, int index, int count);
你可以试试这个:
byte[] b = ...
char[] c = new String(b, "UTF-16").toCharArray();
来自String(byte[] bytes, String charsetName)
:
Constructs a new
String
by decoding the specified array of bytes using the specified charset.
C#API:
public virtual char[] GetChars(byte[] bytes);
Java:
byte[] byte = ...
char[] char = new String(byte, "UTF-16").toCharArray();
Java 方法 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;
}
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;
}
}