首页 > 编程语言 > 详细

java_io学习_编码

时间:2016-06-02 00:37:55      阅读:186      评论:0      收藏:0      [点我收藏+]

package io;

public class encodingDemo{

public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
String s="李雪涛ja";//转换成字节序列用的是项目默认编码
byte[] byte1=s.getBytes();
for(byte b:byte1)
//把字节(转换成了int)以16进制显示
System.out.print(Integer.toHexString(b&0xff)+" ");
System.out.println();
byte[] byte2=s.getBytes("gbk");
//gbk编码英文占1个字节,中文占2个字节
for(byte b:byte2){
System.out.print(Integer.toHexString(b&0xff)+" ");
}
System.out.println();
byte[] byte3=s.getBytes("utf-8");
//utf-8中中文占3个字节,英文占1个字节
for(byte b:byte3)
System.out.print(Integer.toHexString(b&0xff)+" ");
//java是双字节utf-16
byte[] byte4=s.getBytes("utf-16be");
for(byte b:byte4)
System.out.print(Integer.toHexString(b&0xff)+" ");
System.out.println();
/*
* 当你的字节序列是某种编码是,这个时候想把字节序列变为
* 字符串也需要用这种编码方式,否则会乱码
* */
String str1=new String(byte4);
System.out.println(str1);
String str2=new String(byte4,"utf-16be");
System.out.println(str2);
/*
* 文本文件 就是字节序列
* 可以试任意编码的字节序列
* 如果我们在中文机器上直接创建文本文件,那么该文本文件只认识ansi编码
*/
}

}

输出:

c0 ee d1 a9 cc ce 6a 61
c0 ee d1 a9 cc ce 6a 61
e6 9d 8e e9 9b aa e6 b6 9b 6a 61 67 4e 96 ea 6d 9b 0 6a 0 61
gN栮m? j a

李雪涛ja

java_io学习_编码

原文:http://www.cnblogs.com/jasonlixuetao/p/5551540.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!