以下是用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;
}
}
解决方案 »
- POI中为什么第一个setCellValue无效第二个就有效,注释了一个另一个才有效?
- 设计模式是好的,但是为设计模式而设计模式就有点买椟还珠的味道了
- java编一个计算器,遇到问题了,求助
- 谁能帮我解决这个一个字符串变成一维字符串数组的问题
- ^_^散分了~~顺便纠正一个容易犯的小错误^_^
- 如何让新增加的JInternalFrame窗口放在其他JInternalFrame窗口的最前面?
- 谁能提供《java2核心技术》卷I中的corejava这个类给我!
- 请教think in java 里一个小问题?
- 用java编译成.class出德问题,请指点下:
- 送分_____送分
- 利用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自带的转换方式, 自己写一个函数。
你试过没?
你试试转换一个字阿,或是转换“可怜”这个词试试。
谢谢 请教下.....