当前位置: 首页 > 图文教程 > 网络编程 > JSP > jsp中任意文字转Unicode的通用模块

JSP
解决JSP中使用request乱码问题
高性能、高弹性JSP和Servlet性能优化
解决JSP开发Web程序中文显示三种方法
实例讲解在JSP开发中的对象和范围属性
JSP+XML实现网页内容动态显示的方案
weblogic的jsp问题解决方法
JSP处理异常及一些高级主题
JavaBean 在jsp和serlvle中传递技术要点
JSP和Servlet对中文的处理过程
用 servlet 将jsp文件内容转为html
JSP动态网站环境搭建的详细步骤
JSP/Servlet/JSF:自定义标签
Jsp常用功能:CSV文件的生成与分析
Jsp页面实现文件上传下载
JSP网站开发环境的目录结构标准
Windows 2000 server下搭建JSP网站环境
JSP教程:tomcat配置数据库连接池
tomcat下post方式提交服务器乱码
jsp环境下FCKeditor编辑器的应用
JSP网站开发环境配置和使用fckeditor

JSP 中的 jsp中任意文字转Unicode的通用模块


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

  /** ToUnicode.java */
package com.edgewww.util;

import java.io.*;

/**
* 字符串转换成Unicode码的类
* @author 栾金奎 [email protected]
* @date 2001-03-05
*/
public class ToUnicode {

/**
* 把字符串转换成Unicode码
* @param strText 待转换的字符串
* @param code 转换前字符串的编码,如"GBK"
* @return 转换后的Unicode码字符串
*/
public String toUnicode(String strText,String code) throws UnsupportedEncodingException{
  char c;
  String strRet = "" ;
  int intAsc;
  String strHex;
  strText = new String(strText.getBytes("8859_1"),code);
  for ( int i = 0; i < strText.length(); i++ ){
    c = strText.charAt(i);
    intAsc = (int)c;
    if(intAsc>128){
      strHex = Integer.toHexString(intAsc);
      strRet = strRet + "&#x" + strHex+";";
    }
    else{
      strRet = strRet + c;
    }
  }
  return strRet ;
}

}

/** 应用举例 */
/** gbk2Unicode.jsp */
<meta http-equiv="Content-Type" content="text/html; charset=big5">
<jsp:useBean id="g2u" scope="session" class="com.edgewww.util.ToUnicode"/>
<% String lang = "这是简体中文"; %>
<br>
<%=lang %>
<br>
<%=g2u.toUnicode(lang,"GBK") %>