马程序员学习笔记——java常用字符集

来源:互联网 发布:网页视频录制软件 编辑:程序博客网 时间:2024/05/16 18:31

---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------

ASCII :英文一个字节 
gb2312,gbk :中文两个字节,英文一个字节 
在中文系统中ansi一般指gb2312或gbk 
GB2312、GBK都属于双字节字符集 (DBCS) 
Utf-8 :中文三个字节,英文一个字节 
Unicode:中文两个字节,英文两个字 ,java中默认编码

public static void main(String[] args) throws UnsupportedEncodingException {System.out.println("当前系统默认编码:" + Charset.defaultCharset());String str = "我爱abc";System.out.println("----unicode");byte[] bufuni = str.getBytes("unicode");System.out.println(bufuni.length+"字节");for (byte c : bufuni) {System.out.print(c + " ");}System.out.println();System.out.println("----utf-8");byte[] bufs = str.getBytes("utf-8");System.out.println(bufs.length+"字节");for (byte c : bufs) {System.out.print(c + " ");}System.out.println();System.out.println("----gbk");byte[] bufs1 = str.getBytes("gbk");System.out.println(bufs1.length+"字节");for (byte c : bufs1) {System.out.print(c + " ");}System.out.println();System.out.println("----gb2312");byte[] bufgb = str.getBytes("gb2312");System.out.println(bufgb.length+"字节");for (byte c : bufgb) {System.out.print(c + " ");}}
输出结果:

当前系统默认编码:GBK----unicode12字节-2 -1 98 17 114 49 0 97 0 98 0 99 ----utf-89字节-26 -120 -111 -25 -120 -79 97 98 99 ----gbk7字节-50 -46 -80 -82 97 98 99 ----gb23127字节-50 -46 -80 -82 97 98 99 
注意:之所以用Unicode编码时会多出两个字节-2、-1,这是因为系统加上了BOM头(用来告诉计算机当前是哪种编码)


另注:一般windows中文版下的默认编码是gbk,这个可以在windows的语言地域里设置


---------------------- ASP.Net+Unity开发.Net培训、期待与您交流! ----------------------详细请查看:http://edu.csdn.net

0 0
原创粉丝点击