- package com.elmer.c;
- import java.io.UnsupportedEncodingException;
- public class GbktoUtf8 {
- public String convertString(String gbk){
- String utf8 = "";
- try {
- utf8 = new String(gbk2utf8(gbk),"UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return utf8;
- }
- public byte[] gbk2utf8(String chenese) {
- char c[] = chenese.toCharArray();
- byte[] fullByte = new byte[3 * c.length];
- for (int i = 0; i < c.length; i++) {
- int m = (int) c[i];
- String word = Integer.toBinaryString(m);
- StringBuffer sb = new StringBuffer();
- int len = 16 - word.length();
- for (int j = 0; j < len; j++) {
- sb.append("0");
- }
- sb.append(word);
- sb.insert(0, "1110");
- sb.insert(8, "10");
- sb.insert(16, "10");
- String s1 = sb.substring(0, 8);
- String s2 = sb.substring(8, 16);
- String s3 = sb.substring(16);
- byte b0 = Integer.valueOf(s1, 2).byteValue();
- byte b1 = Integer.valueOf(s2, 2).byteValue();
- byte b2 = Integer.valueOf(s3, 2).byteValue();
- byte[] bf = new byte[3];
- bf[0] = b0;
- fullByte[i * 3] = bf[0];
- bf[1] = b1;
- fullByte[i * 3 + 1] = bf[1];
- bf[2] = b2;
- fullByte[i * 3 + 2] = bf[2];
- }
- return fullByte;
- }
- public static void main(String[] args) {
- GbktoUtf8 gtu = new GbktoUtf8();
- System.out.println(gtu.convertString("我是q !"));
- }
- }
核心是找的资料,还没放到web里试过
二.
- public class TranCharset {
- private static final String PRE_FIX_UTF = "&#x";
- private static final String POS_FIX_UTF = ";";
- public TranCharset() {
- }
- /**
- * Translate charset encoding to unicode
- *
- * @param sTemp
- * charset encoding is gb2312
- * @return charset encoding is unicode
- */
- public static String XmlFormalize(String sTemp) {
- StringBuffer sb = new StringBuffer();
- if (sTemp == null || sTemp.equals("")) {
- return "";
- }
- String s = TranCharset.TranEncodeTOGB(sTemp);
- for (int i = 0; i < s.length(); i++) {
- char cChar = s.charAt(i);
- if (TranCharset.isGB2312(cChar)) {
- sb.append(PRE_FIX_UTF);
- sb.append(Integer.toHexString(cChar));
- sb.append(POS_FIX_UTF);
- } else {
- switch ((int) cChar) {
- case 32:
- sb.append(" ");
- break;
- case 34:
- sb.append(""");
- break;
- case 38:
- sb.append("&");
- break;
- case 60:
- sb.append("<");
- break;
- case 62:
- sb.append(">");
- break;
- default:
- sb.append(cChar);
- }
- }
- }
- return sb.toString();
- }
- /**
- * 将字符串编码格式转成GB2312
- *
- * @param str
- * @return
- */
- public static String TranEncodeTOGB(String str) {
- try {
- String strEncode = TranCharset.getEncoding(str);
- String temp = new String(str.getBytes(strEncode), "GB2312");
- return temp;
- } catch (java.io.IOException ex) {
- return null;
- }
- }
- /**
- * 判断输入字符是否为gb2312的编码格式
- *
- * @param c
- * 输入字符
- * @return 如果是gb2312返回真,否则返回假
- */
- public static boolean isGB2312(char c) {
- Character ch = new Character(c);
- String sCh = ch.toString();
- try {
- byte[] bb = sCh.getBytes("gb2312");
- if (bb.length > 1) {
- return true;
- }
- } catch (java.io.UnsupportedEncodingException ex) {
- return false;
- }
- return false;
- }
- /**
- * 判断字符串的编码
- *
- * @param str
- * @return
- */
- public static String getEncoding(String str) {
- String encode = "GB2312";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s = encode;
- return s;
- }
- } catch (Exception exception) {
- }
- encode = "ISO-8859-1";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s1 = encode;
- return s1;
- }
- } catch (Exception exception1) {
- }
- encode = "UTF-8";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s2 = encode;
- return s2;
- }
- } catch (Exception exception2) {
- }
- encode = "GBK";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s3 = encode;
- return s3;
- }
- } catch (Exception exception3) {
- }
- encode = "BIG5";
- try {
- if (str.equals(new String(str.getBytes(encode), encode))) {
- String s4 = encode;
- return s4;
- }
- } catch (Exception exception3) {
- }
- return "";
- }
- public static void main(String args[]) {
- System.out.println(XmlFormalize("下载"));
- }
- }
- package com.lang.string;
- public class ConverFromGBKToUTF8 {
- public static void main(String[] args){
- try {
- ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();
- String chenese = "我是我我们";
- byte [] fullByte = convert.gbk2utf8(chenese);
- String fullStr = new String(fullByte, "UTF-8");
- System.out.println("string from GBK to UTF-8 byte: " + fullStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public byte[] gbk2utf8(String chenese){
- char c[] = chenese.toCharArray();
- byte [] fullByte =new byte[3*c.length];
- for(int i=0; i<c.length; i++){
- int m = (int)c[i];
- String word = Integer.toBinaryString(m);
- // System.out.println(word);
- StringBuffer sb = new StringBuffer();
- int len = 16 - word.length();
- //补零
- for(int j=0; j<len; j++){
- sb.append("0");
- }
- sb.append(word);
- sb.insert(0, "1110");
- sb.insert(8, "10");
- sb.insert(16, "10");
- // System.out.println(sb.toString());
- String s1 = sb.substring(0, 8);
- String s2 = sb.substring(8, 16);
- String s3 = sb.substring(16);
- byte b0 = Integer.valueOf(s1, 2).byteValue();
- byte b1 = Integer.valueOf(s2, 2).byteValue();
- byte b2 = Integer.valueOf(s3, 2).byteValue();
- byte[] bf = new byte[3];
- bf[0] = b0;
- fullByte[i*3] = bf[0];
- bf[1] = b1;
- fullByte[i*3+1] = bf[1];
- bf[2] = b2;
- fullByte[i*3+2] = bf[2];
- }
- return fullByte;
- }
- }
JAVA字符编码系列二:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
1、函数介绍
在Java中,字符串用统一的Unicode编码,每个字符占用两个字节,与编码有关的两个主要函数为:
1)将字符串用指定的编码集合解析成字节数组,完成Unicode-〉charsetName转换
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
2)将字节数组以指定的编码集合构造成字符串,完成charsetName-〉Unicode转换
public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException
2、Unicode与各编码之间的直接转换
下面以对中文字符串"a中文"的编码转换为例,来了解各种编码之间的转换
1)Unicode和GBK
测试结果如下,每个汉字转换为两个字节,且是可逆的,即通过字节可以转换回字符串
String-GBK〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4
ByteArray-GBK〉String:0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u4E2D\u6587(a中文)
2)Unicode和UTF-8
测试结果如下,每个汉字转换为三个字节,且是可逆的,即通过字节可以转换回字符串
String-UTF-8〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87
ByteArray-UTF-8〉String:0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u4E2D\u6587(a中文)
3)Unicode和ISO-8859-1
测试结果如下,当存在汉字时转换失败,非可逆,即通过字节不能再转换回字符串
String-ISO-8859-1〉ByteArray:\u0061\u4E2D\u6587(a中文)-〉0x61 0x3F 0x3F
ByteArray-ISO-8859-1〉String:0x61 0x3F 0x3F-〉\u0061\u003F\u003F(a??)
3、Unicode与各编码之间的交叉转换
在上面直接转换中,由字符串(Unicode)生成的字节数组,在构造回字符串时,使用的是正确的编码集合,如果使用的不是正确的编码集合会怎样呢?会正确构造吗?如果不能正确构造能有办法恢复吗?会信息丢失吗?
下面我们就来看看这种情况,这部分可以说明在某些情况下虽然我们最终正确显示了结果,但其间仍然进行了不正确的转换。
1)能够正确显示的中间不正确转换
我们知道String-GBK〉ByteArray-GBK〉String是正确的,但如果我们采用String-GBK〉ByteArray-ISO-8859-1〉String呢?通过测试结果如下:
String-GBK〉ByteArray-ISO-8859-1〉String:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u00D6\u00D0\u00CE\u00C4(a????)
这时我们得到的字符串为?乱码“a????”,但是通过继续转换我们仍然可以复原回正确的字符串“a中文”,过程如下:
String-GBK〉ByteArray-ISO-8859-1〉String-ISO-8859-1〉ByteArray-GBK〉String
对应:\u0061\u4E2D\u6587(a中文)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u00D6\u00D0\u00CE\u00C4(a????)-〉0x61 0xD6 0xD0 0xCE 0xC4-〉\u0061\u4E2D\u6587(a中文)
也就是我们在首次构造字符串时,我们用了错误的编码集合得到了错误的乱码,但是我们通过错上加错,再用错误的编码集合获取字节数组,然后再用正确的编码集合构造,就又恢复了正确的字符串。这时就属于是“能够正确显示的中间不正确转换”。在Jsp页面提交数据处理时常常发生这种情况。
此外能够正确显示的中间不正确转换还有:
String-UTF-8〉ByteArray-ISO-8859-1〉String-ISO-8859-1〉ByteArray-UTF-8〉String
和
String-UTF-8〉ByteArray-GBK〉String-GBK〉ByteArray-UTF-8〉String
对应:\u0061\u4E2D\u6587(a中文)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u6D93\uE15F\u6783(a涓枃)-〉0x61 0xE4 0xB8 0xAD 0xE6%0x96 0x87-〉\u0061\u4E2D\u6587(a中文)
4、编码过程中错误诊断参考
1)一个汉字对应一个问号
在通过ISO-8859-1从字符串获取字节数组时,由于一个Unicode转换成一个byte,当遇到不认识的Unicode时,转换为0x3F,这样无论用哪种编码构造时都会产生一个?乱码。
2)一个汉字对应两个问号
在通过GBK从字符串获取字节数组时,由于一个Unicode转换成两个byte,如果此时用ISO-8859-1或用UTF-8构造字符串就会出现两个问号。
若是通过ISO-8859-1构造可以再通过上面所说的错上加错恢复(即再通过从ISO-8859-1解析,用GBK构造);
若是通过UTF-8构造则会产生Unicode字符"\uFFFD",不能恢复,若再通过String-UTF-8〉ByteArray-GBK〉String,则会出现杂码,如a锟斤拷锟斤拷
3)一个汉字对应三个问号
在通过UTF-8从字符串获取字节数组时,由于一个Unicode转换成三个byte,如果此时用ISO-8859-1构造字符串就会出现三个问号;用GBK构造字符串就会出现杂码,如a涓枃。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/qinysong/archive/2006/09/05/1179489.aspx