java的String类的getBytes()方法:
public byte[] getBytes() {
return StringCoding.encode(value, offset, count);
}
StringCoding的encode方法
static byte[] encode(char[] ca, int off, int len) {
String csn = Charset.defaultCharset().name();
try {
return encode(csn, ca, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
try {
return encode("ISO-8859-1", ca, off, len);
} catch (UnsupportedEncodingException x) {
// If this code is hit during VM initialization, MessageUtils is
// the only way we will be able to get any kind of error message.
MessageUtils.err("ISO-8859-1 charset not available: "
+ x.toString());
// If we can not find ISO-8859-1 (a required encoding) then things
// are seriously wrong with the installation.
System.exit(1);
return null;
}
}
由此可见,getBytes()方法是受JVM平台字符集限制的。
如果使用String.getBytes(“UTF8”), 也需要注意并不是所有的平台都支持utf-8字符集。
所以最好的方法就是自己封装一个toByteArray方法:
public static byte[] toByteArray(String string) {
byte[] bytes = new byte[string.length()];
char[] chars = string.toCharArray(); f
or (int i = 0; i != chars.length; i++)
{ bytes[i] = (byte)chars[i]; } return bytes; }
和toString方法:
public static String toString(byte[] bytes, int length) {
char[] chars = new char[length];
for (int i = 0; i != chars.length; i++)
{ chars[i] = (char)(bytes[i] & 0xff); } return new String(chars); }