当前位置: 首页 > 图文教程 > 网络编程 > JSP > java big5到gb2312的编码转换

JSP
JavaBeans 程序开发从入门到精通教程
企业级应用中的Applet和Servlet的通信(一)
企业级应用中的Applet和Servlet的通信(三)
企业级应用中的Applet和Servlet的通信 (二)
Web开发中防止浏览器的刷新键引起系统操作重复提交
谈一下关于XHTML网页的制作
40种网页常用小技巧(javascript)←↓------[不时之需]
使用xmlhttp和Java session监听改善站内消息系统
JSP简明教程:行为标签与实例(转
jsp与javascript的结合在页面间传递参数
最基本的一个转换密码字符串为乱码以及解码的程序
55种网页常用小技巧(javascript)
jsp中标签的部署与调用
用jsp动态输出excel文档和中文乱码问题的解决
J2SDK和TOMCAT的安装及配置
web开发中的多条件查询处理技巧1则
JSP连接Mysql数据库攻略
Tomcat的Servlet配制
JSP/Servlet 中的汉字编码问题
Taglib原理和实现 第五章:再论支持El表达式和jstl标签

JSP 中的 java big5到gb2312的编码转换


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 53 ::
收藏到网摘: n/a

将保存在bs数字中的big5编码的字符串数据转换成gb2312编码的数据 此方法将更改原先存储的数据 需要转换的以big5编码的字符串数据 package com.Big5ToUTF8;
import java.io.*;
public class Big5Tran {
private static final String tabFile ="bg-gb.tab";
private static byte[] data;
static{
try{
FileInputStream fis =new FileInputStream(tabFile);
int len =fis.available();
data =new byte[len];
fis.read(data);
fis.close();
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
}
/**
*取得BIG5汉字big在data中的偏移
*/
private static int indexOf(int big){
int high =(big>>>8)&0xff;
int low =big&0xff;
high -= 0xa1;
if(low<=0x7e) low -= 0x40;
else low -= (0xa1 -0x7e -1) +0x40;
return 2*(high*157+low);
}
/**
*将保存在bs数字中的big5编码的字符串数据转换成gb2312编码的数据
*注意:此方法将更改原先存储的数据
*@param bs 需要转换的以big5编码的字符串数据
*@return bs 经过转换的数据,保存在参数中的byte数组中
*/
public static byte[] translateBig5ToGb(byte[] bs){
int index =0;
while(index<bs.length){
int high =bs[index]&0xff;
if(high>=0xa1&&high<=0xfe){
index ++;
if(index>=bs.length) break;
int low =bs[index]&0xff;
if(low<0x40||low>0xfe) continue;
if(low>0x7e&&low<0xa1) continue;
int offset =indexOf((high<<8)|low);
bs[index-1] =data[offset];
bs[index ] =data[offset+1];
index++;
}
else index++;
}
return bs;
}
public static String translateBig5ToGb(String big){
String result =null;
try{
byte[] bs =big.getBytes("big5");
bs =translateBig5ToGb(bs);
result =new String(bs,"gb2312");
}catch(Exception e){
}
return result;
}
}