Common module for converting arbitrary text to Unicode in jsp

Source: Internet
Author: User

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

Import java. io .*;

/**
* String Conversion to Unicode Code Class
* @ Author Jin Kui jsp (preferred for SUN Enterprise Applications) @ shanghai.com
* @ Date 2001-03-05
*/
Public class ToUnicode {

/**
* Convert a string to a Unicode code.
* @ Param strText the string to be converted
* @ Param code the encoding of the string before conversion, such as "GBK"
* @ Return the converted Unicode string
*/
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 & gt; 128 ){
StrHex = Integer. toHexString (intAsc );
StrRet = strRet + "& # x" + strHex + ";";
}
Else {
StrRet = strRet + c;
}
}
Return strRet;
}

}

/** Application example */
/** Gbk2Unicode. jsp (preferred for SUN Enterprise Applications )*/
<Meta http-equiv = "Content-Type" content = "text/html; charset = big5">
<Jsp (preferred for SUN Enterprise Applications): useBean id = "g2u" scope = "session" class = "com. edgewww. util. ToUnicode"/>
<% String lang = "this is simplified Chinese"; %>
<Br>
<% = Lang %>
<Br>
<% = G2u. toUnicode (lang, "GBK") %>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.