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") %>