import java.nio.ByteBuffer;
import java.nio.charset.Charset;
/**
* Charset encoding test. Run the same input string, which contains
* some non-ascii characters, through several Charset encoders and dump out
* the hex values of the resulting byte sequences.
*/
public class DecodeTest {
public static void main(String[] args) {
// This is the character sequence to encode
String input = "\u00bfMa\u00f1ana?";
String [] charsetNames = {
"US-ASCII", "ISO-8859-1", "UTF-8", "UTF-16BE",
"UTF-16LE", "UTF-16"
};
for (int i = 0; i < charsetNames.length; i++) {
doEncode (Charset.forName(charsetNames[i]), input);
}
}
private static void doEncode(Charset cs, String input) {
ByteBuffer bb = cs.encode(input);
System.out.println("Charset: " + cs.name());
System.out.println(" input :" + input);
System.out.println("Encoded: " );
for (int i = 0; bb.hasRemaining(); i++) {
int b = bb.get();
int ival = ((int) b) & 0xff;
char c = (char) ival;
// Keep tabular alignment pretty
if (i < 10) System.out.print(" ");
// Print index number
System.out.print(" " + i + ": ");
// Better formatted output is coming someday...
if (ival < 16) System.out.print("0");
// Print the hex value of the byte
System.out.print(Integer.toHexString(ival));
// If the byte seems to be the value of a
// printable character, print it. No guarantee
// it will be.
if (Character.isWhitespace(c) || Character.isISOControl(c)) {
System.out.println("");
} else {
System.out.println(" (" + c + ")");
}
}
System.out.println("");
}
}
输出结果
Charset: US-ASCII
input :¿Mañana?
Encoded:
0: 3f (?)
1: 4d (M)
2: 61 (a)
3: 3f (?)
4: 61 (a)
5: 6e (n)
6: 61 (a)
7: 3f (?)
Charset: ISO-8859-1
input :¿Mañana?
Encoded:
0: bf (¿)
1: 4d (M)
2: 61 (a)
3: f1 (ñ)
4: 61 (a)
5: 6e (n)
6: 61 (a)
7: 3f (?)
Charset: UTF-8
input :¿Mañana?
Encoded:
0: c2 (Â)
1: bf (¿)
2: 4d (M)
3: 61 (a)
4: c3 (Ã)
5: b1 (±)
6: 61 (a)
7: 6e (n)
8: 61 (a)
9: 3f (?)
Charset: UTF-16BE
input :¿Mañana?
Encoded:
0: 00
1: bf (¿)
2: 00
3: 4d (M)
4: 00
5: 61 (a)
6: 00
7: f1 (ñ)
8: 00
9: 61 (a)
10: 00
11: 6e (n)
12: 00
13: 61 (a)
14: 00
15: 3f (?)
Charset: UTF-16LE
input :¿Mañana?
Encoded:
0: bf (¿)
1: 00
2: 4d (M)
3: 00
4: 61 (a)
5: 00
6: f1 (ñ)
7: 00
8: 61 (a)
9: 00
10: 6e (n)
11: 00
12: 61 (a)
13: 00
14: 3f (?)
15: 00
Charset: UTF-16
input :¿Mañana?
Encoded:
0: fe (þ)
1: ff (ÿ)
2: 00
3: bf (¿)
4: 00
5: 4d (M)
6: 00
7: 61 (a)
8: 00
9: f1 (ñ)
10: 00
11: 61 (a)
12: 00
13: 6e (n)
14: 00
15: 61 (a)
16: 00
17: 3f (?)
UTF -16BE 和UTF -16LE把每个字符编码为一个 2-字节数值。因此这类编码的解码器必须
要预先了解数据是如何编码的,或者根据编码数据流本身来确定字节顺序的方式。UTF -16
编码承认一种字节顺序标记:Unicode字符\uFEFF 。只有发生在编码流的开端时字节顺序
标记才表现为其特殊含义。如果之后遇到该值,它是根据其定义的 Unicode 值(零宽度,
无间断空格)被映射。外来的,小字节序系统可能会优先考虑\ uFEF 并且把流编码为
UTF -16LE。使用UTF -16编码优先考虑和认可字节顺序标记使系统带有不同的内部字节顺
序,从而与 Unicode数据交流
UTF-16BE | 无字节标记,编码高位字序 |
UTF-16LE | 无字节标记,编码低位字序 |
更多信息请参考: orelly出版的 java nio 第6章.
分享到:
相关推荐
本资源利用多个实例,如打印文件路径、输出控制字符、显示特殊字符等,展示了C语言转义字符的用法和作用,以及相关的概念和知识,如ASCII码、字符集、字符串等。本资源适合C语言教学和学习的教师和学生使用,帮助...
你可以看到,每个错误都有3种表示法:即一个消息ID(这是你可以在源代码中使用的一个宏,以便与GetLastError的返回值进行比较),消息文本(对错误的英文描述)和一个号码(你应该避免使用这个号码,而应该使用消息...
本书编写过程中得到了各级领导和机械工业出版社华章分社的大力支持,在此表示衷心的感谢。 限于作者水平,书中难免有不足与疏漏之处,敬请专家、同行及广大读者批评指正! 为便于老师教学,我们将为选用本教材的...
在前面的章节中,我们展示了一些有关如何选取 HTML 元素的实例。 关键点是学习 jQuery 选择器是如何准确地选取您希望应用效果的元素。 jQuery 元素选择器和属性选择器允许您通过标签名、属性名或内容对 HTML 元素...
NEAT 开 发 指南 文档 适用于 PT80 系列 移动数据终端 版本记录 版本号 版本描述 发布日期 V 1.0 初始版本。 2012-04-12 V1.1 修改前三章内容 2012-09-25 目录 第一章 关于本手册.....................................
博客的兴起,让更多的人在网络上,建起了展示自己的空间,同时更多的人愿意在网络上,分享个人的心情日记,个人收藏,这些都是普通的博客内容。 本次论文的目的制作网络日记本,我们要实现网络日记本的增加、删除、...
7.2.3 字符串过滤程序 256 7.2.4 整数溢出 256 7.2.5 类型转换错误 260 7.3 案例研究:IIS索引服务漏洞 262 7.3.1 CVariableSet:: 7.3.1 AddExtensionControlBlock 263 7.3.2 DecodeURLEscapes 267 7.4 结论 271 第8...
Oracle 数据库中的SQL是当今市场上功能最强大的SQL实现之一,而本书全面展示了这一工具的威力。如何才能让更多人有效地学习和掌握SQL呢?Karen Morton及其团队在本书中提供了专业的方案:先掌握语言特性,再学习...
其实还有更简单的方法,而且是更好的方法,使用合理描述参数和SQL语句返回值的接口(比如IUserOperation.class),这样现在就可以至此那个更简单,更安全的代码,没有容易发生的字符串文字和转换的错误.下面是详细...