jsp中任意文字轉(zhuǎn)Unicode的通用模塊
發(fā)布時間:2008-09-22 閱讀數(shù): 次 來源:網(wǎng)樂原科技
/** ToUnicode.java */
package com.edgewww.util;
import java.io.*;
/**
* 字符串轉(zhuǎn)換成Unicode碼的類
* @author 欒金奎 jsp@shanghai.com
* @date 2001-03-05
*/
public class ToUnicode {
/**
* 把字符串轉(zhuǎn)換成Unicode碼
* @param strText 待轉(zhuǎn)換的字符串
* @param code 轉(zhuǎn)換前字符串的編碼,如"GBK"
* @return 轉(zhuǎn)換后的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 ;
}
}
/** 應(yīng)用舉例 */
/** 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") %>