java字符转-48,java字符串应用之字符串编码转换 (转)

一、关键技术点:

1、当前流行的字符编码格式有:US-ASCII、ISO-8859-1、UTF-8、UTF-16BE、UTF-16LE、UTF-16、GBK、GB2312等,其中GBK、GB2312是专门处理中文编码的。

2、String的getBytes方法用于按指定编码获取字符串的字节数组,参数指定了解码格式,如果没有指定解码格式,则按系统默认编码格式。

3、String的“String(bytes[] bs, String charset)”构造方法用于把字节数组按指定的格式组合成一个字符串对象

二、实例演示:

None.gifpackagebook.String;

None.gif

None.gifimportjava.io.UnsupportedEncodingException;

None.gif

ExpandedBlockStart.gif

ContractedBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png * 转换字符串的编码

6a9c071a08f1dae2d3e1c512000eef41.png *@authorjoe

6a9c071a08f1dae2d3e1c512000eef41.png *

ExpandedBlockEnd.gif*/None.gif

ExpandedBlockStart.gif

ContractedBlock.gifpublicclassChangeCharset...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**7位ASCII字符,也叫作ISO646-US、Unicode字符集的基本拉丁块*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString US_ASCII="US-ASCII";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**ISO拉丁字母表 No.1,也叫做ISO-LATIN-1*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString ISO_8859_1="ISO-8859-1";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**8 位 UCS 转换格式*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString UTF_8="UTF-8";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**16 位 UCS 转换格式,Big Endian(最低地址存放高位字节)字节顺序*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString UTF_16BE="UTF-16BE";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**16 位 UCS 转换格式,Litter Endian(最高地址存放地位字节)字节顺序*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString UTF_16LE="UTF-16LE";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**16 位 UCS 转换格式,字节顺序由可选的字节顺序标记来标识*/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString UTF_16="UTF-16";

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**中文超大字符集     **/6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString GBK="GBK";

6a9c071a08f1dae2d3e1c512000eef41.png    

6a9c071a08f1dae2d3e1c512000eef41.pngpublicstaticfinalString GB2312="GB2312";

6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成US-ASCII码*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toASCII(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, US_ASCII);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成ISO-8859-1*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toISO_8859_1(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, ISO_8859_1);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成UTF-8*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toUTF_8(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, UTF_8);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成UTF-16BE*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toUTF_16BE(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, UTF_16BE);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成UTF-16LE*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toUTF_16LE(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, UTF_16LE);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成UTF-16*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toUTF_16(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, UTF_16);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成GBK*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toGBK(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str, GBK);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**将字符编码转换成GB2312*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString toGB2312(String str)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.pngreturnthis.changeCharset(str,GB2312);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * 字符串编码转换的实现方法

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramstr    待转换的字符串

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramnewCharset    目标编码

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString changeCharset(String str, String newCharset)throwsUnsupportedEncodingException...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(str!=null)...{

6a9c071a08f1dae2d3e1c512000eef41.png//用默认字符编码解码字符串。与系统相关,中文windows默认为GB23126a9c071a08f1dae2d3e1c512000eef41.pngbyte[] bs=str.getBytes();

6a9c071a08f1dae2d3e1c512000eef41.pngreturnnewString(bs, newCharset);//用新的字符编码生成字符串ExpandedSubBlockEnd.gif}6a9c071a08f1dae2d3e1c512000eef41.pngreturnnull;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gif/** *//**6a9c071a08f1dae2d3e1c512000eef41.png     * 字符串编码转换的实现方法

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramstr    待转换的字符串

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramoldCharset    源字符集

6a9c071a08f1dae2d3e1c512000eef41.png     *@paramnewCharset    目标字符集

ExpandedSubBlockEnd.gif*/ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicString changeCharset(String str, String oldCharset, String newCharset)throwsUnsupportedEncodingException...{

ExpandedSubBlockStart.gif

ContractedSubBlock.gifif(str!=null)...{

6a9c071a08f1dae2d3e1c512000eef41.png//用源字符编码解码字符串6a9c071a08f1dae2d3e1c512000eef41.pngbyte[] bs=str.getBytes(oldCharset);

6a9c071a08f1dae2d3e1c512000eef41.pngreturnnewString(bs, newCharset);

ExpandedSubBlockEnd.gif        }6a9c071a08f1dae2d3e1c512000eef41.pngreturnnull;

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png    

ExpandedSubBlockStart.gif

ContractedSubBlock.gifpublicstaticvoidmain(String[] args)throwsUnsupportedEncodingException...{

6a9c071a08f1dae2d3e1c512000eef41.png        ChangeCharset test=newChangeCharset();

6a9c071a08f1dae2d3e1c512000eef41.png        String str="This is a 中文的 String!";

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("str:"+str);

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String gbk=test.toGBK(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成GBK码:"+gbk);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String ascii=test.toASCII(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成US-ASCII:"+ascii);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String iso88591=test.toISO_8859_1(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成ISO-8859-1码:"+iso88591);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        gbk=test.changeCharset(iso88591, ISO_8859_1, GBK);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("再把ISO-8859-1码的字符串转换成GBK码:"+gbk);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String utf8=test.toUTF_8(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成UTF-8码:"+utf8);

6a9c071a08f1dae2d3e1c512000eef41.png        String utf16be=test.toUTF_16BE(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成UTF-16BE码:"+utf16be);

6a9c071a08f1dae2d3e1c512000eef41.png        gbk=test.changeCharset(utf16be, UTF_16BE, GBK);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("再把UTF-16BE编码的字符转换成GBK码:"+gbk);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String utf16le=test.toUTF_16LE(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成UTF-16LE码:"+utf16le);

6a9c071a08f1dae2d3e1c512000eef41.png        gbk=test.changeCharset(utf16le, UTF_16LE, GBK);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("再把UTF-16LE编码的字符串转换成GBK码:"+gbk);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println();

6a9c071a08f1dae2d3e1c512000eef41.png        

6a9c071a08f1dae2d3e1c512000eef41.png        String utf16=test.toUTF_16(str);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("转换成UTF-16码:"+utf16);

6a9c071a08f1dae2d3e1c512000eef41.png        String gb2312=test.changeCharset(utf16, UTF_16, GB2312);

6a9c071a08f1dae2d3e1c512000eef41.png        System.out.println("再把UTF-16编码的字符串转换成GB2312码:"+gb2312);

ExpandedSubBlockEnd.gif    }6a9c071a08f1dae2d3e1c512000eef41.png

ExpandedBlockEnd.gif}

输出结果:

None.gifstr:Thisisa 中文的 String!None.gif转换成GBK码:Thisisa 中文的 String!None.gif

None.gif转换成US-ASCII:Thisisa??????String!None.gif

None.gif转换成ISO-8859-1码:Thisisa??????String!None.gif

None.gif再把ISO-8859-1码的字符串转换成GBK码:Thisisa 中文的 String!None.gif

None.gif

None.gif转换成UTF-8码:Thisisa?????String!None.gif转换成UTF-16BE码:周楳?猠愠????瑲楮朡

None.gif再把UTF-16BE编码的字符转换成GBK码:Thisisa 中文的 String!None.gif

None.gif转换成UTF-16LE码:桔獩椠?????匠牴湩Ⅷ

None.gif再把UTF-16LE编码的字符串转换成GBK码:Thisisa 中文的 String!None.gif

None.gif转换成UTF-16码:周楳?猠愠????瑲楮朡

None.gif再把UTF-16编码的字符串转换成GB2312码:?Thisisa 中文的 String!

三、源码分析:

更改字符串编码的步骤为:

1、调用String的getByte方法对字符串进行解码,得到字符串的字节数组(字节数组不携带任何有关编码格式的信息,只有字符才有编码格式)

2、根据字节数组和新的字符编码构造一个新的String对象,得到的就是按照新的字符编码生成的字符串

posted on 2012-02-16 20:48 fly 阅读(186) 评论(0)  编辑  收藏 所属分类: java学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值