String s = "中国人 text here";
byte[] b = s.getBytes("UTF-8");
byte[] c = s.getBytes("GBK");
byte[] b = s.getBytes("UTF-8");
byte[] c = s.getBytes("GBK");
解决方案 »
- 关于正则表达式提取规则文本
- 这样错了吗?
- 谁可以提供给我 坦克大战的 碰撞代码
- JRadioButton的事件问题.............. 在线等.........
- 超级菜的问题。。。。请教各位。。
- 求助 ~AIP43496203AIP34572095AIP73135430
- 有谁能告诉我用SWT的开发包,怎么可以任意控制Composite的大小或SashForm任意窗口的到小。
- 高分求教,关于短信编程的
- 紧急求助!!
- 读取png格式图片出现错误:javax.imageio.IIOException: I/O error reading PNG header!
- 正则表达式问题(JAVASE)
- 想问一问正则表达式在平常开发中会经常用到吗
我是不明白在这里:
在默认的情况下,String构造函数是不是参考ASCII码表?
看源代码不就知道。
public String(byte bytes[]) {
this(bytes, 0, bytes.length);
}
public String(byte bytes[], int offset, int length) {
checkBounds(bytes, offset, length);
this.value = StringCoding.decode(bytes, offset, length);
}
String csn = Charset.defaultCharset().name();
try {
// use charset name decode() variant which provides caching.
return decode(csn, ba, off, len);
} catch (UnsupportedEncodingException x) {
warnUnsupportedCharset(csn);
}
try {
return decode("ISO-8859-1", ba, 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;
}
}
byte[] arr2 = str.getBytes();
for (byte byt : arr2) {
System.out.print(byt+" ");
}
结果 65 66 67 68
猜测ASCII码范围内的字符,不受编码类型的影响。
至少在常见的可见字符(也就是键盘上那些字母,数字,符号),是和ASCII完全兼容的。
也就是说,这些字符集中,如果遇到二进制的01000001(十进制65,十六进制0x41)这个字节,都代表字母A。具体到GBK来说,它单字节部分,也就是高位(左边)第一个bit是0的,代表这个字符,只占用1个字节完全和ASCII兼容(UTF-8也是类似的方式)
http://zh.wikipedia.org/wiki/GBK