以下是用java将GBK字符转成UTF-8编码格式, 那么反过来,如何将UTF-8字符转成GBK编码格式呢?
package com.lang.string;
public class ConverFromGBKToUTF8 {
public static void main(String[] args){
try {
ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();
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;
}
}
package com.lang.string;
public class ConverFromGBKToUTF8 {
public static void main(String[] args){
try {
ConverFromGBKToUTF8 convert = new ConverFromGBKToUTF8();
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 数据库提交的问题 请大家帮忙
- [求助]关于同步关键字的问题
- 高分请教windows上的cvs的若干问题
- 请问:用JAVA如何读取微软CA证书的公钥和私钥?
- 《Thinking in Java》中的reference表示什么??
- default package 中的类能用imort吗?
- 请问JTextArea能存放多少内容?如何保证JTextArea不出现内存溢出?
- 有没有高人用过freejava,拜托,帮个忙!
- SpringMVC+Mybatis字段设置统一值
- 利用socket编程,两台客户机,一台路由器的问题
- project > build automatically 这个是什么意思??
自己先顶一下!!!
String textcontent=new String(request.getParameter("boy").getBytes("GBK")).trim();
str = new String(str.getByte(),"UTF-8");
转化为Java的unicode格式,然后用
str.getByte("GBK");
转化为"GBK"的字节码注意,
1 一个页面里,你只能选择一个。
2 java内部只有unicode编码。
try {
String fullStr = new String(tempString.getBytes("UTF-8"), "GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
也就是不用java自带的转换方式, 自己写一个函数。
你试过没?
你试试转换一个字阿,或是转换“可怜”这个词试试。
谢谢 请教下.....